LoginSignup
0
1

More than 3 years have passed since last update.

PHP: Smarty の使い方

Posted at

Ubuntu 20.04 で確認した方法です。

ライブラリーのダウンロード

wget https://github.com/smarty-php/smarty/archive/v3.1.35.tar.gz

インストール

v3.1.35.tar.gz を解凍して、/usr/local/lib 以下に置く

サンプルプログラム

フォルダー構造

$ tree 
.
├── templates
│   └── hello.tpl
├── templates_c
└── test.php

templates_c で書き込めるようにして置く。777 なら確実に書き込める。

test.php
<?php
define('SMARTY_DIR', '/usr/local/lib/smarty-3.1.35/libs/');
require_once(SMARTY_DIR . 'Smarty.class.php');


$smarty = new Smarty();

$smarty->template_dir = "./templates/";
$smarty->compile_dir = "./templates_c/";

$name = $_GET['name'];

$smarty->assign("name", $name);

$smarty->display("hello.tpl");
?>
hello.tpl
<!DOCTYPE html>
<html lang="ja">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello World!</title>
</head>
<body>
このページは Template です。<p />
<blockquote>
    <h2>こんにちは {$name}!</h2>
</blockquote>
<p />
<hr />
Aug/29/2020 PM 13:22<p />
</body>
</html>

実行結果

test.php?name=新美南吉

smarty_aa.png

test.php?name=宮沢賢治

smarty_bb.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1