4、模板里定義的變量
1 2 3 4 5 | <{$name= 'Bob' }> The value of $name is <{$name}>. output: The value of $name is Bob. |
5、保留變量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $smarty. get $smarty.post $smarty.cookies $smarty.server $smarty.env $smarty.session $smarty.request $smarty.now //當(dāng)前時(shí)間戳 $smarty. const //訪問(wèn)php常量 $smarty.capture //捕獲內(nèi)置的{capture}...{/capture}模版輸出 $smarty.config //取得配置變量。{$smarty.config.foo}是{#foo#}的同義詞 $smarty.section //指向{section}循環(huán)的屬性 $smarty.template //返回經(jīng)過(guò)處理的當(dāng)前模板名 $smarty.current_dir //返回經(jīng)過(guò)處理的當(dāng)前模板目錄名 $smarty.version //返回經(jīng)過(guò)編譯的Smarty模板版本號(hào) |
1 2 3 4 5 6 | <!-- main.tpl --> <{$smarty.now}><br> <{date( 'Y-m-d' ,$smarty.now)}><br> <{$smarty.template }><br> <{$smarty.current_dir }><br> <{$smarty.version }><br> |
變量調(diào)節(jié)器作用于變量、自定義函數(shù)或字符串。變量調(diào)節(jié)器的用法是:‘|’符號(hào)右接調(diào)節(jié)器名稱。變量調(diào)節(jié)器可接收附加參數(shù)影響其行為。參數(shù)位于調(diào)節(jié)器右邊,并用‘:’符號(hào)分開(kāi)
[注意]對(duì)于同一個(gè)變量,可以使用多個(gè)修改器。它們將從左到右按照設(shè)定好的順序被依次組合使用。使用時(shí)必須要用'|'字符作為它們之間的分隔符
capitalize[首字符大寫(xiě)]
將變量里的所有單詞首字大寫(xiě),與php的ucwords()函數(shù)類似。默認(rèn)參數(shù)為false用于確定帶數(shù)字的單詞是否需要大寫(xiě)
1 2 3 4 5 6 7 8 9 10 | <{$articleTitle= 'next x-men film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|capitalize}><br> <{$articleTitle|capitalize: true }> <br> output: next x-men film, x3, delayed. Next X-Men Film, x3, Delayed. Next X-Men Film, X3, Delayed. |
lower[小寫(xiě)]
將變量字符串小寫(xiě),作用等同于php的strtolower()函數(shù)
1 2 3 4 5 6 7 8 | <{$articleTitle= 'Next x-men film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|lower}><br> output: Next x-men film, x3, delayed. next x-men film, x3, delayed. |
upper[大寫(xiě)]
將變量改為大寫(xiě),等同于php的strtoupper()函數(shù)
1 2 3 4 5 6 7 8 | <{$articleTitle= 'Next x-men film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|upper}><br> output: Next x-men film, x3, delayed. NEXT X-MEN FILM, X3, DELAYED. |
cat[連接字符串]
將cat里的值后接到給定的變量后面
1 2 3 | <{$articleTitle= 'next x-men film, x3, delayed.' }> <{$articleTitle|cat: ' yesterday.' }> OUTPUT: next x-men film, x3, delayed. yesterday. |
count_characters[字符計(jì)數(shù)]
計(jì)算變量里的字符數(shù)。默認(rèn)參數(shù)為false,用于確定是否計(jì)算空格字符
1 2 3 4 5 6 7 8 9 | <{$articleTitle= 'next x-men film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|count_characters}><br> <{$articleTitle|count_characters: true }><br> OUTPUT: next x-men film, x3, delayed. 25 29 |
count_paragraphs[計(jì)算段數(shù)]
計(jì)算變量里的段落數(shù)量
1 2 3 4 5 6 7 | <{$articleTitle= 'next x-men\n film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|count_paragraphs}><br> OUTPUT: next x-men film, x3, delayed. 2 |
count_sentences[計(jì)算句數(shù)]
計(jì)算變量里句子的數(shù)量
1 2 3 4 5 6 7 | <{$articleTitle= 'next x-men. film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|count_sentences}><br> OUTPUT: next x-men. film, x3, delayed. 2 |
count_words[計(jì)算詞數(shù)]
計(jì)算變量里的詞數(shù)
1 2 3 4 5 6 7 | <{$articleTitle= 'next x-men film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|count_words}><br> OUTPUT: next x-men film, x3, delayed. 5 |
date_format[格式化日期]
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 27 28 29 30 31 32 33 34 35 | %a - 當(dāng)前區(qū)域星期幾的簡(jiǎn)寫(xiě) %A - 當(dāng)前區(qū)域星期幾的全稱 %b - 當(dāng)前區(qū)域月份的簡(jiǎn)寫(xiě) %B - 當(dāng)前區(qū)域月份的全稱 %c - 當(dāng)前區(qū)域首選的日期時(shí)間表達(dá) %C - 世紀(jì)值(年份除以 100 后取整,范圍從 00 到 99) %d - 月份中的第幾天,十進(jìn)制數(shù)字(范圍從 01 到 31) %D - 和 %m/%d/%y 一樣 %e - 月份中的第幾天,十進(jìn)制數(shù)字,一位的數(shù)字前會(huì)加上一個(gè)空格(范圍從 ' 1' 到 '31' ) %g - 和 %G 一樣,但是沒(méi)有世紀(jì) %G - 4 位數(shù)的年份,符合 ISO 星期數(shù)(參見(jiàn) %V)。和 %V 的格式和值一樣,只除了如果 ISO 星期數(shù)屬于前一年或者后一年,則使用那一年。 %h - 和 %b 一樣 %H - 24 小時(shí)制的十進(jìn)制小時(shí)數(shù)(范圍從 00 到 23) %I - 12 小時(shí)制的十進(jìn)制小時(shí)數(shù)(范圍從 00 到 12) %j - 年份中的第幾天,十進(jìn)制數(shù)(范圍從 001 到 366) %m - 十進(jìn)制月份(范圍從 01 到 12) %M - 十進(jìn)制分鐘數(shù) %n - 換行符 %p - 根據(jù)給定的時(shí)間值為 `am ' 或 `pm' ,或者當(dāng)前區(qū)域設(shè)置中的相應(yīng)字符串 %r - 用 a.m. 和 p.m. 符號(hào)的時(shí)間 %R - 24 小時(shí)符號(hào)的時(shí)間 %S - 十進(jìn)制秒數(shù) %t - 制表符 %T - 當(dāng)前時(shí)間,和 %H:%M:%S 一樣 %u - 星期幾的十進(jìn)制數(shù)表達(dá) [1,7],1 表示星期一 %U - 本年的第幾周,從第一周的第一個(gè)星期天作為第一天開(kāi)始 %V - 本年第幾周的 ISO 8601:1988 格式,范圍從 01 到 53,第 1 周是本年第一個(gè)至少還有 4 天的星期,星期一作為每周的第一天。(用 %G 或者 %g 作為指定時(shí)間戳相應(yīng)周數(shù)的年份組成。) %W - 本年的第幾周數(shù),從第一周的第一個(gè)星期一作為第一天開(kāi)始 %w - 星期中的第幾天,星期天為 0 %x - 當(dāng)前區(qū)域首選的時(shí)間表示法,不包括時(shí)間 %X - 當(dāng)前區(qū)域首選的時(shí)間表示法,不包括日期 %y - 沒(méi)有世紀(jì)數(shù)的十進(jìn)制年份(范圍從 00 到 99) %Y - 包括世紀(jì)數(shù)的十進(jìn)制年份 %Z 或 %z - 時(shí)區(qū)名或縮寫(xiě) %% - 文字上的 `%' 字符 |
1 2 3 4 5 6 | <{$smarty.now|date_format}><br> <{$smarty.now|date_format: '%D' }><br> output: Mar 25, 2017 03/25/17 |
default[默認(rèn)值]
為變量設(shè)置一個(gè)默認(rèn)值。當(dāng)變量未設(shè)置或?yàn)榭兆址畷r(shí),將由給定的默認(rèn)值替代其輸出
1 2 3 4 5 6 7 | <{$articleTitle| default : 'a' }><br> <{$articleTitle= 'next x-men film, x3, delayed.' }> <{$articleTitle| default : 'a' }><br> output: a next x-men film, x3, delayed. |
escape[轉(zhuǎn)義]
escape作用于變量,用以html、url、單引號(hào)、十六進(jìn)制、十六進(jìn)制實(shí)體、javascript、郵件的轉(zhuǎn)碼或轉(zhuǎn)義。第一個(gè)參數(shù)默認(rèn)為'html',可選參數(shù)有'html,htmlall,url,quotes,hex,hexentity,javascript';第二個(gè)參數(shù)默認(rèn)為'utf-8',可選參數(shù)有'ISO-8859-1,UTF-8'...
1 2 3 4 5 6 7 | <{$articleTitle= ''Stiff Opposition Expected to Casketless Funeral Plan'' }> <{$articleTitle|escape}><br> <{$articleTitle|escape: 'url' }> output: 'Stiff Opposition Expected to Casketless Funeral Plan' %27Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan%27 |
indent[縮進(jìn)]
在每行縮進(jìn)字符串,默認(rèn)是4個(gè)字符。對(duì)于第一個(gè)可選參數(shù),可以指定縮進(jìn)字符數(shù),對(duì)于第二個(gè)可選參數(shù),可以指定使用什么字符縮進(jìn),例如'\t'作為tab
1 2 3 4 5 6 7 | <{$articleTitle= ''Stiff Opposition Expected to Casketless Funeral Plan'' }> <{$articleTitle}><br> <{$articleTitle|indent}> output: 'Stiff Opposition Expected to Casketless Funeral Plan' 'Stiff Opposition Expected to Casketless Funeral Plan' |
nl2br[換行符替換成<br />]
所有的換行符將被替換成 <br />,功能同PHP中的nl2br()函數(shù)一樣
1 2 3 4 5 6 7 8 | <{$articleTitle= 'Next x-men\nfilm, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|nl2br}><br> output: Next x-men film, x3, delayed. Next x-men film, x3, delayed. |
regex_replace[正則替換]
使用正則表達(dá)式在變量中搜索和替換,語(yǔ)法來(lái)自php的preg_replace()函數(shù)
1 2 3 4 5 6 7 8 | <{$articleTitle= 'Next x-men\nfilm, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|regex_replace: '/[\r\t\n]/' : ' ' }><br> output: Next x-men film, x3, delayed. Next x-men film, x3, delayed. |
replace[替換]
一種在變量中進(jìn)行簡(jiǎn)單的搜索和替換字符串的處理。等同于php的str_replace()函數(shù)
1 2 3 4 5 6 7 | <{$articleTitle= 'Next x-men film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|replace: 'x' : 'y' }><br> output: Next x-men film, x3, delayed. Neyt y-men film, y3, delayed. |
spacify[插空]
插空是一種在變量的字符串的每個(gè)字符之間插入空格或者其他的字符(串)的方法
1 2 3 4 5 6 7 8 9 | <{$articleTitle= 'Next x-men film, x3, delayed.' }> <{$articleTitle}><br> <{$articleTitle|spacify}><br> <{$articleTitle|spacify: '^' }><br> output: Next x-men film, x3, delayed. N e x t x - m e n f i l m , x 3 , d e l a y e d . N^e^x^t^ ^x^-^m^e^n^ ^f^i^l^m^,^ ^x^3^,^ ^d^e^l^a^y^e^d^. |
string_format[字符串格式化]
一種格式化字符串的方法,例如格式化為十進(jìn)制數(shù)等等。實(shí)際運(yùn)用的是php的sprintf()函數(shù)
1 2 3 4 5 6 7 8 9 | <{$number=23.5678}> <{$number}><br> <{$number|string_format: '%.2f' }><br> <{$number|string_format: '%d' }> output: 23.5678 23.57 23 |
strip[去除(多余空格)]
用一個(gè)空格或一個(gè)給定字符替換所有重復(fù)空格、換行和制表符
strip_tags[去除html標(biāo)簽]
去除<和>標(biāo)簽,包括在<和>之間的全部?jī)?nèi)容
1 2 3 4 5 6 7 | <{$articleTitle= 'Blind Woman Gets New Kidney from Dad she Hasn't Seen in <b>years</b>.' }> <{$articleTitle}><br> <{$articleTitle|strip_tags}><br> output: Blind Woman Gets New Kidney from Dad she Hasn't Seen in <b>years</b>.<br> Blind Woman Gets New Kidney from Dad she Hasn't Seen in years .<br> |
truncate[截取]
從字符串開(kāi)始處截取某長(zhǎng)度的字符,默認(rèn)是80個(gè),也可以指定第二個(gè)參數(shù)作為追加在截取字符串后面的文本串。該追加字串被計(jì)算在截取長(zhǎng)度中。默認(rèn)情況下,smarty會(huì)截取到一個(gè)詞的末尾。如果想要精確的截取多少個(gè)字符,把第三個(gè)參數(shù)改為'true';第四個(gè)參數(shù)默認(rèn)設(shè)置為FALSE,表示將截取至字符串末尾,設(shè)置為T(mén)RUE則截取到中間。注意如果設(shè)了TRUE,則忽略字符邊界
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <{$articleTitle= 'Two Sisters Reunite after Eighteen Years at Checkout Counter.' }> <{$articleTitle}><br> <{$articleTitle|truncate}><br> <{$articleTitle|truncate:30}><br> <{$articleTitle|truncate:30: '' }><br> <{$articleTitle|truncate:30: '---' }><br> <{$articleTitle|truncate:30: '' : true }><br> <{$articleTitle|truncate:30: '...' : true }><br> <{$articleTitle|truncate:30: '..' : true : true }><br> output: Two Sisters Reunite after Eighteen Years at Checkout Counter. Two Sisters Reunite after Eighteen Years at Checkout Counter. Two Sisters Reunite after... Two Sisters Reunite after Two Sisters Reunite after--- Two Sisters Reunite after Eigh Two Sisters Reunite after E... Two Sisters Re..ckout Counter. |
wordwrap[行寬約束]
可以指定段落的列寬(也就是一行多少個(gè)字符,超過(guò)這個(gè)字符數(shù)換行),默認(rèn)80。第二個(gè)參數(shù)可選,指定在約束點(diǎn)使用什么換行符,默認(rèn)為'\n'。默認(rèn)情況下smarty將截取到詞尾,如果想精確到設(shè)定長(zhǎng)度的字符,請(qǐng)將第三個(gè)參數(shù)設(shè)為ture。本調(diào)節(jié)器等同于php的wordwrap()函數(shù)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <{$articleTitle= 'Blind woman gets new kidney from dad she hasn't seen in years.' }> <{$articleTitle}><br> <{$articleTitle|wordwrap:30}><br> <{$articleTitle|wordwrap:20}><br> <{$articleTitle|wordwrap:30: '<br />\n' }><br> <{$articleTitle|wordwrap:26: '\n' : true }><br> output: Blind woman gets new kidney from dad she hasn't seen in years.<br> Blind woman gets new kidney from dad she hasn't seen in years.<br> Blind woman gets new kidney from dad she hasn't seen in years.<br> Blind woman gets new kidney<br /> from dad she hasn't seen in <br /> years.<br> Blind woman gets new kidney from dad she hasn't seen in years.<br> |
{$var=...} 變量賦值
這是{assign}函數(shù)的簡(jiǎn)寫(xiě)版,可以直接賦值給模版,也可以為數(shù)組元素賦值
1 2 3 4 | <{$name= 'Bob' }>The value of $name is <{$name}>. output: The value of $name is Bob. |
{append} 追加
{append}用于在模板執(zhí)行期間建立或追加模板變量數(shù)組
1 2 3 4 5 6 7 8 9 10 11 | <{append var = 'name' value= 'Bob' index= 'first' }> <{append var = 'name' value= 'Meyer' index= 'last' }> <{* 或者 *}> <{append 'name' 'Bob' index= 'first' }> <{* 簡(jiǎn)寫(xiě) *}> <{append 'name' 'Meyer' index= 'last' }> <{* 簡(jiǎn)寫(xiě) *}> The first name is <{$name.first}>.<br> The last name is <{$name.last}>. output: The first name is Bob.The last name is Meyer. |
{assign} 賦值
{assign}用來(lái)在模板運(yùn)行時(shí)為模板變量賦值
<{assign var='name' value='Bob'}><{assign 'name' 'Bob'}> <{* 簡(jiǎn)寫(xiě) *}>The value of $name is <{$name}>. output:The value of $name is Bob.
{config_load}
{config_load}用來(lái)從配置文件中加載config變量(#variables#)到模版
{for} 循環(huán)
{for}、{forelse}標(biāo)簽用來(lái)創(chuàng)建一個(gè)簡(jiǎn)單循環(huán),支持以下不同的格式:
{for $var=$start to $end}步長(zhǎng)為1的簡(jiǎn)單循環(huán);
{for $var=$start to $end step $step}其它步長(zhǎng)循環(huán)
當(dāng)循環(huán)無(wú)迭代時(shí)執(zhí)行{forelse}
1 <ul> 2 <{for $foo=1 to 3}> 3 <li><{$foo}></li><{/for}> 4 </ul> 5 6 output: 7 <ul> 8 <li>1</li> 9 <li>2</li>10 <li>3</li>11 </ul>
{while}循環(huán)
隨著一些特性加入到模版引擎,Smarty的{while}循環(huán)與php的while語(yǔ)句一樣富有彈性。每一個(gè){while}必須與一個(gè){/while}成對(duì)出現(xiàn),所有php條件和函數(shù)在它身上同樣適用,諸如||、or、&&、and、is_array()等等
下面是一串有效的限定符,它們的左右必須用空格分隔開(kāi),注意列出的清單中方括號(hào)是可選的,在適用情況下使用相應(yīng)的等號(hào)(全等或不全等)
{foreach},{foreachelse}遍歷
{foreach}用來(lái)遍歷數(shù)據(jù)數(shù)組,{foreach}與{section}循環(huán)相比更簡(jiǎn)單、語(yǔ)法更干凈,也可以用來(lái)遍歷關(guān)聯(lián)數(shù)組
1 {foreach $arrayvar as $itemvar}2 {foreach $arrayvar as $keyvar=>$itemvar}
{foreach}循環(huán)可以嵌套;數(shù)組變量通常是(另)一個(gè)數(shù)組的值,用來(lái)指導(dǎo)循環(huán)的次數(shù),可以為專有循環(huán)傳遞一個(gè)整數(shù);當(dāng)數(shù)組變量無(wú)值時(shí)執(zhí)行{foreachelse};
{foreach}的屬性是@index、@iteration、@first、@last、@show、@total;
可以用循環(huán)項(xiàng)目中的當(dāng)前鍵({$item@key})代替鍵值變量
@index:包含當(dāng)前數(shù)組的下標(biāo),開(kāi)始時(shí)為0
@iteration:包含當(dāng)前循環(huán)的迭代,總是以1開(kāi)始,這點(diǎn)與index不同。每迭代一次值自動(dòng)加1
@first:當(dāng){foreach}循環(huán)第一個(gè)時(shí)first為真
@last:當(dāng){foreach}迭代到最后時(shí)last為真
@show:檢測(cè){foreach}循環(huán)是否無(wú)數(shù)據(jù)顯示,show是個(gè)布爾值(true or false)
@total:包含{foreach}循環(huán)的總數(shù)(整數(shù)),可以用在{forach}里面或后面
{break}:停止/終止數(shù)組迭代
{continue}:中止當(dāng)前迭代而開(kāi)始下一個(gè)迭代/循環(huán)
1 <{$myColors['a'] = 'red'}> 2 <{$myColors['b'] = 'green'}> 3 <{$myColors['c'] = 'blue'}> 4 <{$myColors['d'] = 'pink'}> 5 <{$myColors['e'] = 'yellow'}> 6 <ul> 7 <{foreach $myColors as $color}> 8 <{if $color@first}> 9 <li><b><{$color@iteration}>:<{$color@index}>:<{$color@key}>:<{$color}></b></li>10 <{elseif $color@last}>11 <li><b><{$color@iteration}>:<{$color@index}>:<{$color@key}>:<{$color}></b></li>12 <{else}>13 <li><{$color@iteration}>:<{$color@index}>:<{$color@key}>:<{$color}></li>14 <{/if}>15 <{foreachelse}>16 no result... 17 <{/foreach}>18 </ul> 19 20 output:21 <ul>22 <li><b>1:0:a:red</b></li>23 <li>2:1:b:green</li>24 <li>3:2:c:blue</li>25 <li>4:3:d:pink</li>26 <li><b>5:4:e:yellow</b></li> 27 </ul>
{if}{elseif}{else} 條件
隨著一些特性加入到模版引擎,Smarty的{if}語(yǔ)句與php的if語(yǔ)句一樣富有彈性。每一個(gè){if}必須與一個(gè){/if}成對(duì)出現(xiàn),允許使用{else}和{elseif},所有php條件和函數(shù)在這里同樣適用,諸如||、or、&&、and、is_array()等等
{include}
{include}標(biāo)簽用于在當(dāng)前模板中包含其它模板。當(dāng)前模板中的任何有效變量在被包含模板中同樣可用
必須指定file屬性,該屬性指明模板資源的位置
變量可以作為屬性參數(shù)傳遞給被包含模板,任何明確傳遞給被包含模板的變量只在被包含文件的作用域中有效。如果傳遞的屬性變量在當(dāng)前模板中有同名變量,那么傳遞的屬性變量將覆蓋當(dāng)前模板變量
1 2 3 4 5 6 7 8 9 10 11 | <!-- main.tpl --> <{include file= 'header.tpl' test= '小火柴' }> <!-- header.tpl --> <{$test}> <{$test= 'aaa' }><br> <{$test}> output: 小火柴 aaa |
{function}
{function}用來(lái)在模板中創(chuàng)建函數(shù),可以像調(diào)用插件函數(shù)一樣調(diào)用它們
我們不寫(xiě)一個(gè)表達(dá)內(nèi)容的插件,而是讓它保留在模板中,通常這是個(gè)更易于管理的選擇。同時(shí),它也簡(jiǎn)化了對(duì)數(shù)據(jù)的遍歷,例如深度嵌套菜單。另外可以在模板中直接使用{funcname...}函數(shù)。
{function}標(biāo)簽必須包含模板函數(shù)名的name屬性,該name標(biāo)簽名必須能夠調(diào)用模板函數(shù)
默認(rèn)變量值應(yīng)能作為屬性傳遞到模板函數(shù),當(dāng)模板函數(shù)被調(diào)用的時(shí)候,默認(rèn)值應(yīng)能被復(fù)寫(xiě)
在模板函數(shù)內(nèi)部應(yīng)能使用被調(diào)用模板的所有變量值,在模板函數(shù)中更改或新建變量的值必須具局部作用域,而且在執(zhí)行模板函數(shù)后這些變量值在被調(diào)用模板內(nèi)部應(yīng)不可見(jiàn)
調(diào)用函數(shù)時(shí),可以直接使用函數(shù)名,或者使用{call}
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!-- main.tpl --> <{function name=test a=0 b=0}> <{$a}>+<{$b}>=<{$a+$b}> <{/function}> <{test}><br> <{test a=1 b=2}><br> <{call test a=3 b=3}><br> output: 0+0=0 1+2=3 3+3=6 |
Smarty中的插件總是按需加載。只有在模板腳本中調(diào)用特定的調(diào)節(jié)器、函數(shù)、資源插件等時(shí)才會(huì)自動(dòng)加載。此外,每個(gè)插件只加載一次,即便在同一個(gè)請(qǐng)求中存在幾個(gè)不同的Smarty實(shí)例同時(shí)運(yùn)行
插件目錄可以是一個(gè)包含路徑的字符串或包含多個(gè)路徑的數(shù)組。安裝插件的時(shí)候,將插件簡(jiǎn)單地置于其中一個(gè)目錄下,Smarty會(huì)自動(dòng)識(shí)別使用
插件文件和函數(shù)必須遵循特定的命名約定以便Smarty識(shí)別
插件文件必須命名如下:
type.name.php
其中type為下面這些插件類型中的一種:
name為合法標(biāo)識(shí)符,僅包含字母、數(shù)字和下劃線
function.html_select_date.php, resource.db.php, modifier.spacify.php
插件內(nèi)的函數(shù)應(yīng)遵循如下命名約定
如果調(diào)節(jié)器(modifier)命名為foo,那么按規(guī)則函數(shù)為smarty_modifier_foo()。如果指定的插件文件不存在或文件、函數(shù)命名不合規(guī)范,Smarty會(huì)輸出對(duì)應(yīng)的錯(cuò)誤信息
Smarty既可自動(dòng)從文件系統(tǒng)加載插件,也可在運(yùn)行時(shí)通過(guò)register_* API函數(shù)注冊(cè)插件。當(dāng)然,也可以通過(guò)unregister_* API函數(shù)卸載已經(jīng)載入的插件
對(duì)于只在運(yùn)行時(shí)注冊(cè)的插件函數(shù)不必遵守命名約定
如果某個(gè)插件依賴其它插件的某些功能(事實(shí)上,一些插件被綁定在Smarty中),那么可以通過(guò)如下方法加載需要的插件:
1 2 3 | <?php require_once $smarty->_get_plugin_filepath( 'function' , 'html_options' ); ?> |
按照慣例,Smarty對(duì)象通常作為最后一個(gè)參數(shù)傳遞給插件,但有兩個(gè)例外:1、調(diào)節(jié)器不須接受Smarty對(duì)象的傳遞;2、為了向前兼容舊版Smarty,塊插件將$repeat排在Smarty對(duì)象后面作為最后一個(gè)參數(shù)($smarty作為倒數(shù)第二個(gè)參數(shù))
【模板函數(shù)】
1 2 3 | void smarty_function_name($ params , $smarty); array $ params ; object $smarty; |
模板傳遞給模板函數(shù)的所有屬性都包含在關(guān)聯(lián)數(shù)組$params中
在模板中,函數(shù)的輸出內(nèi)容(返回值)在原位置用函數(shù)標(biāo)簽代替,例如{fetch}函數(shù)。作為另一種選擇,函數(shù)也可以單純地用來(lái)做些非輸出內(nèi)容的任務(wù),如{assign}函數(shù)
如果函數(shù)需要分配(俗話說(shuō)的賦值)一些變量給模板或者使用Smarty提供的一些函數(shù),可以通過(guò)$smarty對(duì)象實(shí)現(xiàn),如$smarty->foo()
1 2 3 4 5 6 7 8 9 10 11 12 | //function.eightball.php <?php function smarty_function_eightball($ params , $smarty){ $answers = array( 'Yes' , 'No' , 'No way' , 'Outlook not so good' , 'Ask again soon' , 'Maybe in your reality' ); $result = array_rand($answers); return $answers[$result];} ?> <!-- main.tpl --> Question: Will we ever have time travel?<br> Answer: <{eightball}>. |
除了使用以上方式,還可以使用registerPlugin()方式來(lái)進(jìn)行插件注冊(cè),但是由于與PHP代碼混合在一起,不建議使用
registerPlugin()
void registerPlugin(string type, string name, mixed callback, bool cacheable, mixed cache_attrs);
registerPlugin()方法在腳本中注冊(cè)函數(shù)或方法作為插件。其參數(shù)如下:
“type”定義插件的類型,其值為下列之一:“function”、“block”、“compiler”和“modifier”
“name”定義插件的函數(shù)名
“callback”為定義的php回調(diào)函數(shù),其類型為下列之一:
1、包含函數(shù)名的字符串;
2、格式為(&$object, $method)的數(shù)組,其中,&$object為引用對(duì)象,$method為包含方法名的字符串;
3、格式為($class, $method)的數(shù)組,其中,$class為類名,$method為類中的方法。
“cacheable”和“cache_attrs”參數(shù)大多情況下可以省略
【調(diào)節(jié)器】
調(diào)節(jié)器是一些簡(jiǎn)短的函數(shù),這些函數(shù)被應(yīng)用于顯示模板前作用于一個(gè)變量,或者其它情形中。調(diào)節(jié)器可以連接起來(lái)(執(zhí)行)
mixed smarty_modifier_name($value, $param1);mixed $value;[mixed $param1, ...];
調(diào)節(jié)器插件的第一個(gè)參數(shù)應(yīng)該直接了當(dāng)?shù)芈暶魈幚硎裁搭愋停梢允亲址?、?shù)組、對(duì)象等等這些類型)。其它的參數(shù)是可選的,取決于執(zhí)行的操作類型。調(diào)節(jié)器必須返回處理結(jié)果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //modifier.u.php <?php function smarty_modifier_u($str){ return ucwords($str); } ?> <!-- main.tpl --> <{$testValue = 'Question: Will we ever have time travel?' }><br> <{$testValue}><br> <{$testValue|u}><br> output: Question: Will we ever have time travel? Question: Will We Ever Have Time Travel? |
【塊函數(shù)】
1 2 3 4 5 | void smarty_block_name($ params , $content, $smarty, &$repeat); array $ params ; mixed $content; object $smarty; boolean &$repeat; |
塊函數(shù)的形式是這樣的:{func} .. {/func}。換句話說(shuō),他們被封閉在一個(gè)模板區(qū)域內(nèi),然后對(duì)該區(qū)域的內(nèi)容進(jìn)行操作。塊函數(shù)優(yōu)先于同名的自定義函數(shù),換句話說(shuō),不能同時(shí)使用自定義函數(shù){func}和塊函數(shù){func} .. {/func}。
默認(rèn)地,函數(shù)實(shí)現(xiàn)會(huì)被Smarty調(diào)用兩次:一次是在開(kāi)始標(biāo)簽,另一次是在閉合標(biāo)簽
從Smarty3.1開(kāi)始打開(kāi)標(biāo)簽回調(diào)(函數(shù))的返回值同樣會(huì)被顯示
只有塊函數(shù)的開(kāi)始標(biāo)簽具有屬性。所有屬性包含在作為關(guān)聯(lián)數(shù)組的$params變量中,經(jīng)由模板傳遞給模板函數(shù)。當(dāng)處理閉合標(biāo)簽時(shí),函數(shù)同樣可訪問(wèn)開(kāi)始標(biāo)簽的屬性
$content變量值取決于函數(shù)是被開(kāi)始標(biāo)簽調(diào)用還是被閉合標(biāo)簽調(diào)用。假如是開(kāi)始標(biāo)簽,變量值將為NULL,如果是閉合標(biāo)簽,$content變量值為模板塊的內(nèi)容。請(qǐng)注意這時(shí)模板塊已經(jīng)被Smarty處理過(guò),因此所接收到的是模板的輸出而不是模板資源
&$repeat參數(shù)通過(guò)引用傳遞給函數(shù)執(zhí)行,并為其提供控制塊顯示多少次的可能性。默認(rèn)情況下,在首次調(diào)用塊函數(shù)(塊開(kāi)始標(biāo)簽)時(shí),&$repeat變量為true,在隨后的所有塊函數(shù)(閉合標(biāo)簽)調(diào)用中其值始終為false。函數(shù)每次執(zhí)行返回的&$repeat值為true時(shí),{func} .. {/func}之間的內(nèi)容會(huì)被求值,同時(shí)參數(shù)$content里的新塊內(nèi)容會(huì)再次調(diào)用執(zhí)行函數(shù)
如果嵌套了塊函數(shù),可以通過(guò)$smarty->_tag_stack變量訪問(wèn)找出父塊函數(shù)。只須對(duì)塊函數(shù)運(yùn)行一下var_dump(),函數(shù)結(jié)構(gòu)就可以一目了然了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //block.s.php <?php function smarty_block_s($ params , $content, $smarty, &$repeat){ return substr($content,0,$ params [ 'num' ]+1); } ?> <!-- main.tpl --> <{$testValue = 'Question: Will we ever have time travel?' }><br> <{$testValue}><br> <{s num= '5' }> <{$testValue}> <{/s}> output: Question: Will we ever have time travel? Quest |
繼承帶來(lái)了模板面向?qū)ο蟾拍睿╫op),它允許定義一個(gè)或多個(gè)基模板供子模板繼承。繼承意味著子模板可覆蓋所有或部份父模板中命名相同的塊區(qū)域
模板繼承是一種編譯時(shí)進(jìn)程,其將建立一個(gè)獨(dú)立的編譯模板文件。與對(duì)應(yīng)的基于載入{include}子模板解決方案相比,當(dāng)解釋模板時(shí),前者有更好的性能
{extends} 繼承
{extends}標(biāo)簽用在模板繼承中子模版對(duì)父模板的繼承
{extends}標(biāo)簽用在模版中的第一行
如果子模板用{extends}標(biāo)簽繼承父模板,那么它只能包含{block}標(biāo)簽(內(nèi)容),其它任何模板內(nèi)容都將忽略
使用此語(yǔ)法為模板資源繼承$template_dir目錄外的文件
<!-- parent.tpl--><!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Document</title></head><body> 我是父模板中的文字 </body></html><!-- child.tpl--><{extends 'parent.tpl'}>
{block} 塊
{block}用來(lái)給模板繼承定義一個(gè)模板資源的命名區(qū)域。子模板的{block}資源區(qū)域?qū)?huì)取代父模板中的相應(yīng)區(qū)域。{block}可以嵌套
任意的子、父模板{block}區(qū)域可以彼此結(jié)合。可以通過(guò)子{block}定義使用append、prepend選項(xiàng)標(biāo)記追加或預(yù)置父{block}內(nèi)容。使用{$smarty.block.parent}可將父模板的{block}內(nèi)容插入至子{block}內(nèi)容中的任何位置。使用{$smarty.block.child}可將子模板{block}內(nèi)容插入至父{block}內(nèi)容中的任何位置
[注意]子模板不能定義任何內(nèi)容,除了需要覆蓋父模板的{block}標(biāo)簽塊,所有在{block}標(biāo)簽外的內(nèi)容將被自動(dòng)移除
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 | <!-- parent.tpl--> <!DOCTYPE html> <html lang= 'en' > <head> <meta charset= 'UTF-8' > <title>Document</title> </head> <body> <{block name= 'one' }> one <{/block}><br> <{block name= 'two' }> two <{/block}><br> <{block name= 'three' }> three <{/block}><br> <{block name= 'four' }> four <{$smarty.block.child}> <{/block}><br> <{block name= 'five' }> five <{/block}><br> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!-- child.tpl--> <{extends 'parent.tpl' }> <{block name= 'one' }> 1 <{/block}><br> <{block name= 'two' prepend}> 2 <{/block}><br> <{block name= 'three' append}> 3 <{/block}><br> <{block name= 'four' }> 4 <{/block}><br> <{block name= 'five' }> 5 <{$smarty.block.parent}> <{/block}><br> block區(qū)域之外的內(nèi)容不會(huì)顯示 |
1 2 3 4 5 6 7 8 9 10 11 12 | <!-- parent.php--> <?php header( 'content-type:text/html;charset=utf-8' ); require './init.inc.php' ; $smarty -> display( 'parent.tpl' ); ?> <!-- child.php--> <?php header( 'content-type:text/html;charset=utf-8' ); require './init.inc.php' ; $smarty -> display( 'child.tpl' ); ?> |
聯(lián)系客服