![]() Memo/PHP/Smartyhttps://dexlab.net:443/pukiwiki/index.php?Memo/PHP/Smarty |
Smarty †プラグイン †Tips †年月のセレクトボックスを表示 †
Smartyのコンパイル済みテンプレートファイル名が衝突した場合の解決法について †compile_dir が一つで、複数のtemplate_dirがある場合に、テンプレートファイル名が重複する場合がある。その場合は、同じキャッシュ済みテンプレートファイルが読み込まれるため表示がおかしくなる。
全てのPHP関数を「暗黙の」修飾子として利用できます †
URLにリンクを張るプラグイン †テンプレート内で変数を割り当てるには †<!--{assign var="名前" value=変数名または定数}--> 指定回数のループを行いたい †例えば、0〜6までループしたい場合は {section name="myLoop" start=0 loop=6} index={$smarty.section.myLoop.index} {/section} テンプレート実行が遅い場合 †sectionを使わずに、foreachが使えるか検討する。 遅い書き方 <!--{section name=loop loop=$DATA->obj1[0]->obj2[1]->obj3}--> <!--{$DATA->obj1[0]->obj2[1]->obj3[loop]}--> <!--{/section}--> 速い書き方 <!--{foreach key=key item=item from=$DATA->obj1[0]->obj2[1]->obj3}--> <!--{$item}--> <!--{/foreach 複雑な変数をassignしている場合、sectionでは添え字しか取得できないため、
添え字による参照しか行えず、複雑になるほど速度が悪化する。 Smartyとは? †PHP用テンプレートエンジンらしい いいところ
インストール †
位置 C:\user\www\htdocs\php\sample\smarty_demo | index.php <- プログラム本体 +---cache +---configs +---templates | index.tpl <- テンプレート \---templates_c index.php <? define('SMARTY_DIR','C:/user/www/htdocs/lib/Smarty/libs/'); define('ROOT_DIR','C:/user/www/htdocs/smarty'); require(SMARTY_DIR.'Smarty.class.php'); $smarty = new Smarty; $smarty->template_dir = ROOT_DIR . '/templates/'; $smarty->compile_dir = ROOT_DIR . '/templates_c/'; $smarty->config_dir = ROOT_DIR . '/configs/'; $smarty->cache_dir = ROOT_DIR . '/cache/'; $smarty->assign('name','Ned'); $smarty->display('index.tpl'); ?> index.tpl {* Smarty *} Hello, {$name}! ブラウザから、index.phpをアクセスしてみます。 リンク † |
|