{if $smarty.request.dateYear != ''}
{assign var="selected_date" value=$smarty.request.dateYear|cat:"-"|cat:$smarty.request.dateMonth|cat:"-01"}
{/if}
{html_select_date prefix="date" display_days=false month_format="%m" field_order="YMD" start_year="-10" reverse_years=true time=$selected_date}
compile_dir が一つで、複数のtemplate_dirがある場合に、テンプレートファイル名が重複する場合がある。その場合は、同じキャッシュ済みテンプレートファイルが読み込まれるため表示がおかしくなる。
$smarty->compile_id = '異なる値';
{$value|substr:3:4}
{$value|number_format}Smartyは、全てのPHP関数を「暗黙の」修飾子として利用できます。number_formatも
{$value|number_format}
と書けば修飾子扱いされるので、プラグインを作らずとも簡単にカンマ区切りができますよ。誰もツッコまないみたいなので参考までにコメントさせていただきました。
以前、個人的にSmartyの講師をやった際のPDFがありますので、もしよろしかったらどうぞ。(かなり初心者向けの内容ですが)
<!--{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では添え字しか取得できないため、
添え字による参照しか行えず、複雑になるほど速度が悪化する。
foreachでは直接値がコピーされるため、添え字参照による速度低下を押さえられる。
これは、コンパイル後のテンプレートを見てみるとよく分かる。
PHP用テンプレートエンジンらしい
本家からDLできる
一度テンプレートをコンパイルし、それをキャッシュするため早い
いいところ
C:\user\www\htdocs\lib\Smarty +---demo | +---configs | \---templates +---libs | +---core | \---plugins \---misc
cache <- キャッシュ configs <- Smarty用の設定ファイル templates <- テンプレートを格納(拡張子はtpl) templates_c <- コンパイル済みテンプレートが自動作成される
位置
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をアクセスしてみます。
{$name}! が 「Ned」に置き換えられ、
「Hello, Ned!」と表示されれば成功です。