PHPでsmartyを使用する
1 2 3 4 5 6 7 8 9 10 11 |
ドキュメントルート ---+--- index.html | +--- SampleApp/ ---+--- index.php | +--- templates/ | +--- templates_c/ | +--- configs/ | +--- cache/ |
ここで、きちんと Smarty が動作するかサンプルを作ってみましょう。以下の内容で、ロジック・ファイルindex.phpを作成:
1 2 3 4 5 6 7 8 9 10 11 |
<?php require_once('Smarty/Smarty.class.php'); $smarty = new Smarty; $smarty->assign('name', 'kamotora'); $smarty->assign('url', 'https://kamotora.net/'); $smarty->display('index.tpl'); ?> |
以下の内容で、テンプレート・ファイル index.tpl
を作成:
1 2 3 4 5 6 7 8 9 10 11 |
<html> <head> <title>User Info</title> </head> <body> 名前:{$name}<br> URL: <a href="{$url}">{$url}</a> </body> </html> |
作成したファイルを以下のように配置:
1 2 3 4 5 6 7 8 9 10 11 |
ドキュメントルート ---+--- index.html | +--- SampleApp/ ---+--- index.php | +--- templates/ ------ index.tpl | +--- templates_c/ | +--- configs/ | +--- cache/ |
Web ブラウザで、http://<server-name>/SampleApp/index.php
へアクセスすると画面が表示されます。