smartyテンプレート使ってると、「アレ?アレなんだっけアレアレ!」てなる。
最低限これだけ見ておれば出来るアレ。
文字列調整
文字数を代入する {assign var=変数名 value=登録内容}
1 | {assign var =foo value= "あいうえおかきくけこさしすせそ" } |
文字数を調整する {変数|mb_strimwidth:始位置:数:省略字:エンコード}
1 2 | {assign var =foo value= "あいうえおかきくけこさしすせそ" } { $foo |mb_strimwidth:0:5: "…" :utf8|escape} |
この結果は『あいうえお…』
タグ生成系
※下記PHP側で$XXX = YYY;て書いている場合は、$smarty->assign('XXX',$XXX);すること前提。
※styleやidやsizeなど生成されるタグに付けたい場合、下記のようにsmartyタグに付ければ良い
1 | {html_options name=aaa options= $bbb selected= $ccc id= "ddd" style= "margin:5px;" multiple= "multiple" } |
プルダウン {html_options name=タグname options=内容 selected=選択値}
PHP側
1 2 | $param_options = array (1=> 'aaa' ,2=> 'bb' ,33=> 'ccc' ); $select_value = 2; |
テンプレート側
1 | {html_options name=send_name options= $param_options selected= $select_value } |
で
1 2 3 4 5 | < select name = "send_name" > < option label = "aaa" value = "1" >aaa</ option > < option label = "bb" value = "2" selected = "selected" >bb</ option > < option label = "ccc" value = "33" >ccc</ option > </ select > |
こういう表示になる
内容補填型プルダウン {html_options name=タグname options=内容 selected=選択値}
『未選択』という項目を追加したい場合、必要な部分だけ追加することが出来る。
PHP側
PHP側
1 2 | $param_options = array (1=> 'aaa' ,2=> 'bb' ,33=> 'ccc' ); $select_value = 2; |
テンプレート側
1 2 3 4 | <select name= "send_name" > <option label= "-未選択-" value= "" >-未選択-</option> {html_options options= $param_options selected= $select_value } </select> |
で
1 2 3 4 5 6 | < select name = "send_name" > < option label = "-未選択-" value = "" >-未選択-</ option > < option label = "aaa" value = "1" >aaa</ option > < option label = "bb" value = "2" selected = "selected" >bb</ option > < option label = "ccc" value = "33" >ccc</ option > </ select > |
注意するべきは『name』を抜くこと。
グループ型プルダウン {html_options name=タグname options=内容 selected=選択値}
『未選択』という項目を追加したい場合、必要な部分だけ追加することが出来る。
PHP側
PHP側
1 2 3 | $param_options [ 'test1' ] = array (1=> 'aaa' ,2=> 'bb' ,33=> 'ccc' ); $param_options [ 'test2' ] = array (55=> 'ddddd' ,100=> 'eeee' ); $select_value = array (2,55); |
テンプレート側
1 | {html_options name=send_name multiple=multiple options= $param_options selected= $select_value } |
で
1 2 3 4 5 6 7 8 9 10 11 | < select name = "send_name" multiple = "multiple" > < optgroup label = "test1" > < option label = "aaa" value = "1" >aaa</ option > < option label = "bb" value = "2" selected = "selected" >bb</ option > < option label = "ccc" value = "33" >ccc</ option > </ optgroup > < optgroup label = "test2" > < option label = "ddddd" value = "55" selected = "selected" >ddddd</ option > < option label = "eeee" value = "100" >eeee</ option > </ optgroup > </ select > |
ラジオボタン {html_radios name=タグname options=内容 selected=選択値}
PHP側
1 2 | $param_options = array (1=> 'aaa' ,2=> 'bb' ,33=> 'ccc' ); $select_value = 2; |
テンプレート側
1 | {html_radios name=send_name options= $param_options selected= $select_value } |
で
1 2 3 | < label >< input type = "radio" name = "send_name" value = "1" />aaa</ label > < label >< input type = "radio" name = "send_name" value = "2" checked = "checked" />bb</ label > < label >< input type = "radio" name = "send_name" value = "33" />ccc</ label > |
こういう表示になる
因みに
1 | separator= '<br />' |
をSmartyタグに追加すると、各『>/label<』の後ろに『>br /<』が付く。
チェックボックス {html_checkboxes name=タグname options=内容 selected=選択値}
PHP側
1 2 | $param_options = array (1=> 'aaa' ,2=> 'bb' ,33=> 'ccc' ); $select_value = array (2,33); |
テンプレート側
1 | {html_checkboxes name=send_name options= $param_options selected= $select_value } |
で
1 2 3 | < label >< input type = "checkbox" name = "send_name[]" value = "1" />aaa</ label > < label >< input type = "checkbox" name = "send_name[]" value = "2" checked = "checked" />bb</ label > < label >< input type = "checkbox" name = "send_name[]" value = "33" checked = "checked" />ccc</ label > |
こういう表示になる
因みに
1 | separator= '<br />' |
をSmartyタグに追加すると、各『>/label<』の後ろに『>br /<』が付く。
繰り返し
内容(配列)から生成型 {foreach~}~{/foreach}
PHP側
1 | $param_options = array (1=> 'aaa' ,2=> 'bb' ,33=> 'ccc' ); |
テンプレート側
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | { foreach name=l_name from= $param_options item=l_item key=l_key} { if $smarty . foreach .l_name.first}最初のループ<br />{/ if } { if $smarty . foreach .l_name.last} 最後のループ<br /> total(合計値):{ $smarty . foreach .l_name.total}<br /> {/ if } key:{ $l_key }<br /> item:{ $l_item }<br /> index(0~):{ $smarty . foreach .l_name.index}<br /> iteration(1~):{ $smarty . foreach .l_name.iteration}<br /> <br /> {foreachelse} 中身が無い場合表示されるもの {/ foreach } |
で
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 最初のループ< br />key:1< br /> item:aaa< br /> index(0~):0< br /> iteration(1~):1< br /> < br /> key:2< br /> item:bb< br /> index(0~):1< br /> iteration(1~):2< br /> < br /> 最後のループ< br /> total(合計値):3< br /> key:33< br /> item:ccc< br /> index(0~):2< br /> iteration(1~):3< br /> < br /> |
こういう表示になる
内容(配列)から生成型 {section~}~{/section}
PHP側
1 | $param_options = array ( 'aaa' , 'bb' , 'ccc' ); |
テンプレート側
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | {section name=l_name loop= $param_options } { if $smarty .section.l_name.first}最初のループ<br />{/ if } { if $smarty .section.l_name.last} 最後のループ<br /> total(合計値):{ $smarty .section.l_name.total}<br /> {/ if } index_prev(前):{ $smarty .section.l_name.index_prev}<br /> index_next(後):{ $smarty .section.l_name.index_next}<br /> index:{ $smarty .section.l_name.index}<br /> iteration:{ $smarty .section.l_name.iteration}<br /> rownum:{ $smarty .section.l_name.rownum}<br /> loop:{ $smarty .section.l_name.loop}<br /> { $param_options [l_name]}<br /> <br /> {sectionelse} 中身が無い場合表示されるもの {/section} |
で
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 最初のループ< br />index_prev(前):-1< br /> index_next(後):1< br /> index:0< br /> iteration:1< br /> rownum:1< br /> loop:3< br /> aaa< br /> < br /> index_prev(前):0< br /> index_next(後):2< br /> index:1< br /> iteration:2< br /> rownum:2< br /> loop:3< br /> bb< br /> < br /> 最後のループ< br /> total(合計値):3< br /> index_prev(前):1< br /> index_next(後):3< br /> index:2< br /> iteration:3< br /> rownum:3< br /> loop:3< br /> ccc< br /> < br /> |
こういう表示になる
※連想配列は使えない。0スタートの配列でないと{$param_options[l_name]}で値が取れない。
回数指定形(for的な使い方) {section~}~{/section}
テンプレート側
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | {section name=l_name start=1 loop=10 step=2 max=3} { if $smarty .section.l_name.first}最初のループ<br />{/ if } { if $smarty .section.l_name.last} 最後のループ<br /> total(合計値):{ $smarty .section.l_name.total}<br /> {/ if } index_prev(前):{ $smarty .section.l_name.index_prev}<br /> index_next(後):{ $smarty .section.l_name.index_next}<br /> index:{ $smarty .section.l_name.index}<br /> iteration:{ $smarty .section.l_name.iteration}<br /> rownum:{ $smarty .section.l_name.rownum}<br /> loop:{ $smarty .section.l_name.loop}<br /> <br /> {sectionelse} 中身が無い場合表示されるもの {/section} |
で
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 最初のループ< br />index_prev(前):-1< br /> index_next(後):3< br /> index:1< br /> iteration:1< br /> rownum:1< br /> loop:10< br /> < br /> index_prev(前):1< br /> index_next(後):5< br /> index:3< br /> iteration:2< br /> rownum:2< br /> loop:10< br /> < br /> 最後のループ< br /> total(合計値):3< br /> index_prev(前):3< br /> index_next(後):7< br /> index:5< br /> iteration:3< br /> rownum:3< br /> loop:10< br /> < br /> |
※start(初期値)、step(刻み)、max(最大値)は別に書かなくてもイイ
※for的な使い方が出来る
雑談
他のテンプレートの種類
OSSとかCMSとかフレームワークとか結構Smartyだし
PHPといえば、『Smarty』と思い勝ちだから
実はこんなにあるんだぜ、ってことでリストUPしてみた。
PHPといえば、『Smarty』と思い勝ちだから
実はこんなにあるんだぜ、ってことでリストUPしてみた。
- Smarty
- patTemplate
- PHPLIB
- HTML_Template_Flexy ※高速らしい
- PEAR Templates
- PHP Fast Template
- phemplate
- Simple Template
- php_template
- bTemplate
※あくまでリスト。説明は割愛するので、各々調べてくださいな。
コメント