【smarty】smartyでHelloWorld
公開日:
:
最終更新日:2013/04/18
smarty
まずはHello World
1.ライブラリをダウンロード
smarty download
2.スクリプトから参照可能な場所にlibsフォルダを配置
C:/xampp/Smarty-3.1.11/libs
3.テンプレート配置フォルダとコンパイルファイル配置フォルダを作成
./smarty/templates
./smarty/templates_c
4.テンプレートを作成してtemplatesに配置
./smarty/templates/index.tpl
<h1>{$message}</h1>
5.テンプレートを埋め込む
index.php
<html> <body> <?php //smartyをインポート require_once "C:/xampp/Smarty-3.1.11/libs/Smarty.class.php"; //セットアップ $smarty = new Smarty(); $smarty->template_dir = dirname(__FILE__)."/smarty/templates"; $smarty->compile_dir = dirname(__FILE__)."/smarty/templates_c"; /* * テンプレート変数に値をセットします。 * ここでは、titleというテンプレート変数に Hello World! という文字列をセットしています。 * テンプレート内では、{$message} という変数でセットした文字列を呼び出すことができます。 */ $smarty->assign("title", "Hello World!"); /* * テンプレートを出力します。出力するテンプレートファイル名を渡します。 * Hello World! と出力されていたら成功です。 */ $smarty->display("index.tpl"); //呼び出した分だけ出力される $smarty->display("index.tpl"); $smarty->display("index.tpl"); ?> </body> </html>
その他できること
設定ファイルで定数を定義
configs/test.conf
# コメントは「#」で始める # 1つ目のセクションまでに宣言したものはグローバル変数となる url = "www.hoge.com" title = "Smarty" #セクション [MySection1] title = "MySection1" [MySection2] title = "MySection2"
index.php
//設定ファイルパスを設定 $smarty->config_dir = dirname(__FILE__)."/smarty/configs";
test.tpl
//設定ファイルの読み込み {config_load file="設定ファイル名"} //変数の表示1 <p>{#変数名#}</p> //変数の表示2 <p>{$smarty.config.変数名}</p>
条件分岐
{if 条件式1} 条件式1trueの場合 {elseif 条件式2} 条件式1がfalseで条件式2がtrueの場合 {else} 上記以外 {/if}
繰り返し foreach
$data = array( "name"=>"Yamada", "old"=>"24", "address"=>"Tokyo" ); $smarty->assign('配列名', array( "name"=>"Yamada", "old"=>"24", "address"=>"Tokyo" ));
roop.tmpl
//keyとnameは省略可 {foreach from=$配列名 item=値 key=キー name=ループ名} {if $smarty.foreach.ループ名.first} <p>最初の要素:{$var}</p> {elseif $smarty.foreach.ループ名.last} <p>最後の要素:{$var}</p> {else} <p>({$smarty.foreach.ループ名.iteration})$キー = {$値}</p> {foreachelse} from属性に指定した変数の値が存在しない場合の処理 {/foreach} <p>total = $smarty.foreach.ループ名.total </p>
繰り返し for
{for $hoge=0 to 9} <li>{$hoge}</li> {/for}
生成したhtmlをキャッシュ
修飾子
smartyはフォーマットを指定して変数を表示することができます。
フォーマットを特定するものが修飾子で
{$変数名|修飾子}
のように使います。
詳しくはPHPBookを参照のこと。
その他(予約変数)
詳しくはPHPBookを参照のこと。
ひととおりのサンプル
index.php
<html> <body> <?php //smartyをインポート require_once "C:/xampp/Smarty-3.1.11/libs/Smarty.class.php"; //セットアップ $smarty = new Smarty(); $smarty->template_dir = dirname(__FILE__)."/smarty/templates"; $smarty->compile_dir = dirname(__FILE__)."/smarty/templates_c"; $smarty->config_dir = dirname(__FILE__)."/smarty/configs"; $smarty->cache_dir = dirname(__FILE__)."/smarty/cache"; //代入 $smarty->assign("title", "Hello World!"); //出力 $smarty->display("index.tpl"); ?>
index.tpl
{* コメント *} <html> <body> //変数を表示 {$title} //設定ファイルの読み込み {config_load file="設定ファイル名"} //変数の表示1 <p>{#変数名#}</p> {if 条件式1} 条件式1trueの場合 {elseif 条件式2} 条件式1がfalseで条件式2がtrueの場合 {else} 上記以外 {/if} //keyとnameは省略可 {foreach from=$配列名 item=値 key=キー name=ループ名} {if $smarty.foreach.ループ名.first} <p>最初の要素:{$var}</p> {elseif $smarty.foreach.ループ名.last} <p>最後の要素:{$var}</p> {else} <p>({$smarty.foreach.ループ名.iteration})$キー = {$値}</p> {foreachelse} from属性に指定した変数の値が存在しない場合の処理 {/foreach} <p>total = $smarty.foreach.ループ名.total </p> </body> </html>
関連記事
記事はありませんでした