PHPにSmartyをセットアップ
約 2 分
目次
概要
PHPにSmartyをセットアップをするメモ
試した環境
PHP 7.1.19 Smarty 3.1.33
Smartyについて
古くからあるPHPのテンプレートエンジンです。
Smarty と twig のトレンド比較
PHPのテンプレートエンジンとして有名なSmartyですが、最近はbaldeやtwigという名前もよく見かけるようになりました。
ダウンロード
Smartyの最新版はGitHubからzipかtarで落としてきます。
https://github.com/smarty-php/smarty/releases
smartyの配置
落として解凍したsmartyディレクトリを任意の場所に配置します。 実装するときに絶対パスで参照するのであれば、どの場所でも問題ないとは思いますが、 include_pathで設定されている場所に配置するのがベターだと思います。
Smarty呼び出し
index.php
include_once("smarty-3.1.33/libs/Smarty.class.php");
// smartyオブジェクト生成
$smarty = new Smarty();
// 設定
$smarty->template_dir = "/path/to/template/"; //相対パスでも可
$smarty->compile_dir = "/path/to/cache/"; //相対パスでも可
// 変数をセット
$smarty->assign("test", "Hello World!");
// テンプレートを読み込んでレンダリング
$smarty->display('index.tpl');
index.tpl
<!DOCTYPE>
<html>
<head>
</head>
<body>
{$test}
</body>
</html>
Macであればターミナルから
php -S localhost:3333 index.php
を実行し、「Hello World!」と表示されていれば成功です。
おすすめの記事
最新の記事
よく読まれている記事
タグから探す
- javascript130
- typescript67
- linux54
- node.js54
- amazon-aws48
- 画像処理48
- アルゴリズム37
- canvas35
- html529
- 画像処理100本ノック27
- php24
- centos24
- python22
- 競技プログラミング21
- mac21
- mysql20
- opencv17
- 雑談16
- 機械学習16
- docker16
- wordpress15
- atcoder14
- apache12
- データベース12
- amazon-s312
- red-hat12
- prisma12
- ubuntu11
- github10
- git10
- react10
- mariadb10
- vue.js9
- next.js9
- aws-cdk9
- css38
- 可視化8
- 小ネタ8
- nestjs8
- amazon-lightsail7
- ブログ6
- cms6
- oracle6
- perl6
- gitlab6
- iam5
- amazon-ec25
- 資格試験5
- aws-amplify5
- curl4