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 //當前時間戳
$smarty. const //訪問php常量
$smarty.capture //捕獲內(nèi)置的{capture}...{/capture}模版輸出
$smarty.config //取得配置變量。{$smarty.config.foo}是{#foo#}的同義詞
$smarty.section //指向{section}循環(huán)的屬性
$smarty.template //返回經(jīng)過處理的當前模板名
$smarty.current_dir //返回經(jīng)過處理的當前模板目錄名
$smarty.version //返回經(jīng)過編譯的Smarty模板版本號
|
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é)器 變量調(diào)節(jié)器作用于變量、自定義函數(shù)或字符串。變量調(diào)節(jié)器的用法是:‘|’符號右接調(diào)節(jié)器名稱。變量調(diào)節(jié)器可接收附加參數(shù)影響其行為。參數(shù)位于調(diào)節(jié)器右邊,并用‘:’符號分開 [注意]對于同一個變量,可以使用多個修改器。它們將從左到右按照設(shè)定好的順序被依次組合使用。使用時必須要用'|'字符作為它們之間的分隔符 capitalize[首字符大寫] 將變量里的所有單詞首字大寫,與php的ucwords()函數(shù)類似。默認參數(shù)為false用于確定帶數(shù)字的單詞是否需要大寫 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[小寫] 將變量字符串小寫,作用等同于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[大寫] 將變量改為大寫,等同于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[字符計數(shù)] 計算變量里的字符數(shù)。默認參數(shù)為false,用于確定是否計算空格字符 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[計算段數(shù)] 計算變量里的段落數(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[計算句數(shù)] 計算變量里句子的數(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[計算詞數(shù)]
計算變量里的詞數(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 - 當前區(qū)域星期幾的簡寫
%A - 當前區(qū)域星期幾的全稱
%b - 當前區(qū)域月份的簡寫
%B - 當前區(qū)域月份的全稱
%c - 當前區(qū)域首選的日期時間表達
%C - 世紀值(年份除以 100 后取整,范圍從 00 到 99)
%d - 月份中的第幾天,十進制數(shù)字(范圍從 01 到 31)
%D - 和 %m/%d/%y 一樣
%e - 月份中的第幾天,十進制數(shù)字,一位的數(shù)字前會加上一個空格(范圍從 ' 1' 到 '31' )
%g - 和 %G 一樣,但是沒有世紀
%G - 4 位數(shù)的年份,符合 ISO 星期數(shù)(參見 %V)。和 %V 的格式和值一樣,只除了如果 ISO 星期數(shù)屬于前一年或者后一年,則使用那一年。
%h - 和 %b 一樣
%H - 24 小時制的十進制小時數(shù)(范圍從 00 到 23)
%I - 12 小時制的十進制小時數(shù)(范圍從 00 到 12)
%j - 年份中的第幾天,十進制數(shù)(范圍從 001 到 366)
%m - 十進制月份(范圍從 01 到 12)
%M - 十進制分鐘數(shù)
%n - 換行符
%p - 根據(jù)給定的時間值為 `am ' 或 `pm' ,或者當前區(qū)域設(shè)置中的相應(yīng)字符串
%r - 用 a.m. 和 p.m. 符號的時間
%R - 24 小時符號的時間
%S - 十進制秒數(shù)
%t - 制表符
%T - 當前時間,和 %H:%M:%S 一樣
%u - 星期幾的十進制數(shù)表達 [1,7],1 表示星期一
%U - 本年的第幾周,從第一周的第一個星期天作為第一天開始
%V - 本年第幾周的 ISO 8601:1988 格式,范圍從 01 到 53,第 1 周是本年第一個至少還有 4 天的星期,星期一作為每周的第一天。(用 %G 或者 %g 作為指定時間戳相應(yīng)周數(shù)的年份組成。)
%W - 本年的第幾周數(shù),從第一周的第一個星期一作為第一天開始
%w - 星期中的第幾天,星期天為 0
%x - 當前區(qū)域首選的時間表示法,不包括時間
%X - 當前區(qū)域首選的時間表示法,不包括日期
%y - 沒有世紀數(shù)的十進制年份(范圍從 00 到 99)
%Y - 包括世紀數(shù)的十進制年份
%Z 或 %z - 時區(qū)名或縮寫
%% - 文字上的 `%' 字符
|
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[默認值] 為變量設(shè)置一個默認值。當變量未設(shè)置或為空字符串時,將由給定的默認值替代其輸出 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、單引號、十六進制、十六進制實體、javascript、郵件的轉(zhuǎn)碼或轉(zhuǎn)義。第一個參數(shù)默認為'html',可選參數(shù)有'html,htmlall,url,quotes,hex,hexentity,javascript';第二個參數(shù)默認為'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[縮進] 在每行縮進字符串,默認是4個字符。對于第一個可選參數(shù),可以指定縮進字符數(shù),對于第二個可選參數(shù),可以指定使用什么字符縮進,例如'\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[正則替換] 使用正則表達式在變量中搜索和替換,語法來自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[替換] 一種在變量中進行簡單的搜索和替換字符串的處理。等同于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[插空] 插空是一種在變量的字符串的每個字符之間插入空格或者其他的字符(串)的方法 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[字符串格式化] 一種格式化字符串的方法,例如格式化為十進制數(shù)等等。實際運用的是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[去除(多余空格)] 用一個空格或一個給定字符替換所有重復(fù)空格、換行和制表符 1 <{$articleTitle='Grandmother of\neight makes\t hole in one.'}> 2 <{$articleTitle}><br> 3 <{$articleTitle|strip}><br> 4 <{$articleTitle|strip:' '}><br> 5 6 output: 7 Grandmother of 8 eight makes hole in one. 9 Grandmother of eight makes hole in one.10 Grandmother of eight makes hole in one. strip_tags[去除html標簽] 去除<和>標簽,包括在<和>之間的全部內(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[截取] 從字符串開始處截取某長度的字符,默認是80個,也可以指定第二個參數(shù)作為追加在截取字符串后面的文本串。該追加字串被計算在截取長度中。默認情況下,smarty會截取到一個詞的末尾。如果想要精確的截取多少個字符,把第三個參數(shù)改為'true';第四個參數(shù)默認設(shè)置為FALSE,表示將截取至字符串末尾,設(shè)置為TRUE則截取到中間。注意如果設(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[行寬約束]
可以指定段落的列寬(也就是一行多少個字符,超過這個字符數(shù)換行),默認80。第二個參數(shù)可選,指定在約束點使用什么換行符,默認為'\n'。默認情況下smarty將截取到詞尾,如果想精確到設(shè)定長度的字符,請將第三個參數(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>
|
內(nèi)置函數(shù){$var=...} 變量賦值 這是{assign}函數(shù)的簡寫版,可以直接賦值給模版,也可以為數(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' }> <{* 簡寫 *}>
<{append 'name' 'Meyer' index= 'last' }> <{* 簡寫 *}>
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}用來在模板運行時為模板變量賦值 <{assign var='name' value='Bob'}><{assign 'name' 'Bob'}> <{* 簡寫 *}>The value of $name is <{$name}>. output:The value of $name is Bob. {config_load} {config_load}用來從配置文件中加載config變量(#variables#)到模版 foo.conf:[a]x=1[b]x=2[c]x=3<!-- main.tpl --><{config_load file='foo.conf' section='a'}><{#x#}>output:1 {for} 循環(huán) {for}、{forelse}標簽用來創(chuàng)建一個簡單循環(huán),支持以下不同的格式: {for $var=$start to $end}步長為1的簡單循環(huán); {for $var=$start to $end step $step}其它步長循環(huán) 當循環(huán)無迭代時執(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> 1 <ul> 2 <{for $foo=2 to 10 max=3}> 3 <li><{$foo}></li><{/for}> 4 </ul> 5 6 output: 7 <ul> 8 <li>2</li> 9 <li>3</li>10 <li>4</li>11 </ul> {while}循環(huán) 隨著一些特性加入到模版引擎,Smarty的{while}循環(huán)與php的while語句一樣富有彈性。每一個{while}必須與一個{/while}成對出現(xiàn),所有php條件和函數(shù)在它身上同樣適用,諸如||、or、&&、and、is_array()等等 下面是一串有效的限定符,它們的左右必須用空格分隔開,注意列出的清單中方括號是可選的,在適用情況下使用相應(yīng)的等號(全等或不全等) 1 {while $foo > 0} 2 {$foo--} 3 {/while} {foreach},{foreachelse}遍歷 {foreach}用來遍歷數(shù)據(jù)數(shù)組,{foreach}與{section}循環(huán)相比更簡單、語法更干凈,也可以用來遍歷關(guān)聯(lián)數(shù)組 1 {foreach $arrayvar as $itemvar}2 {foreach $arrayvar as $keyvar=>$itemvar} {foreach}循環(huán)可以嵌套;數(shù)組變量通常是(另)一個數(shù)組的值,用來指導(dǎo)循環(huán)的次數(shù),可以為專有循環(huán)傳遞一個整數(shù);當數(shù)組變量無值時執(zhí)行{foreachelse}; {foreach}的屬性是@index、@iteration、@first、@last、@show、@total; 可以用循環(huán)項目中的當前鍵({$item@key})代替鍵值變量 1 <{$myColors['a'] = 'red'}> 2 <{$myColors['b'] = 'green'}> 3 <{$myColors['c'] = 'blue'}> 4 <ul> 5 <{foreach $myColors as $color}> 6 <li><{$color@key}>:<{$color}></li> 7 <{/foreach}> 8 </ul> 9 10 output:11 <ul>12 <li>a:red</li>13 <li>b:green</li>14 <li>c:blue</li> 15 </ul> @index:包含當前數(shù)組的下標,開始時為0 @iteration:包含當前循環(huán)的迭代,總是以1開始,這點與index不同。每迭代一次值自動加1 @first:當{foreach}循環(huán)第一個時first為真 @last:當{foreach}迭代到最后時last為真 @show:檢測{foreach}循環(huán)是否無數(shù)據(jù)顯示,show是個布爾值(true or false) @total:包含{foreach}循環(huán)的總數(shù)(整數(shù)),可以用在{forach}里面或后面 {break}:停止/終止數(shù)組迭代 {continue}:中止當前迭代而開始下一個迭代/循環(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}語句與php的if語句一樣富有彈性。每一個{if}必須與一個{/if}成對出現(xiàn),允許使用{else}和{elseif},所有php條件和函數(shù)在這里同樣適用,諸如||、or、&&、and、is_array()等等 <{if $name == 'Fred' || $name == 'Wilma'}><br> ... <br><{/if}> <br><{* 允許使用圓括號 *}> <br><{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}> <br>... <br><{/if}> <br><{* 可以嵌入函數(shù) *}><br> <{if count($var) gt 0}> <br>... <br><{/if}> <br><{* 數(shù)組檢查 *}> <br><{if is_array($foo) }> <br>..... <br><{/if}> <br><{* 是否空值檢查 *}> <br><{if isset($foo) }> <br>..... <br><{/if}> <br><{* 測試值為偶數(shù)還是奇數(shù) *}> <br><{if $var is even}> <br>... <br><{/if}> <br><{if $var is odd}> <br>... <br><{/if}> <br><{if $var is not odd}> <br>... <br><{/if}> <br><{* 測試var能否被4整除 *}> <br><{if $var is div by 4}> <br>... <br><{/if}> <br><{* 測試發(fā)現(xiàn)var是偶數(shù),2個為一組,也就是0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, 等等 *}> <br><{if $var is even by 2}> <br>... <br><{/if}> <br><{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}> <br><{if $var is even by 3}> <br>... <br><{/if}> {include}
{include}標簽用于在當前模板中包含其它模板。當前模板中的任何有效變量在被包含模板中同樣可用
必須指定file屬性,該屬性指明模板資源的位置
變量可以作為屬性參數(shù)傳遞給被包含模板,任何明確傳遞給被包含模板的變量只在被包含文件的作用域中有效。如果傳遞的屬性變量在當前模板中有同名變量,那么傳遞的屬性變量將覆蓋當前模板變量 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}用來在模板中創(chuàng)建函數(shù),可以像調(diào)用插件函數(shù)一樣調(diào)用它們
我們不寫一個表達內(nèi)容的插件,而是讓它保留在模板中,通常這是個更易于管理的選擇。同時,它也簡化了對數(shù)據(jù)的遍歷,例如深度嵌套菜單。另外可以在模板中直接使用{funcname...}函數(shù)。
{function}標簽必須包含模板函數(shù)名的name屬性,該name標簽名必須能夠調(diào)用模板函數(shù)
默認變量值應(yīng)能作為屬性傳遞到模板函數(shù),當模板函數(shù)被調(diào)用的時候,默認值應(yīng)能被復(fù)寫
在模板函數(shù)內(nèi)部應(yīng)能使用被調(diào)用模板的所有變量值,在模板函數(shù)中更改或新建變量的值必須具局部作用域,而且在執(zhí)行模板函數(shù)后這些變量值在被調(diào)用模板內(nèi)部應(yīng)不可見
調(diào)用函數(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ù)、資源插件等時才會自動加載。此外,每個插件只加載一次,即便在同一個請求中存在幾個不同的Smarty實例同時運行 插件目錄可以是一個包含路徑的字符串或包含多個路徑的數(shù)組。安裝插件的時候,將插件簡單地置于其中一個目錄下,Smarty會自動識別使用 插件文件和函數(shù)必須遵循特定的命名約定以便Smarty識別 插件文件必須命名如下: 其中type為下面這些插件類型中的一種: function modifier block compiler prefilter postfilter outputfilter resource insert name為合法標識符,僅包含字母、數(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會輸出對應(yīng)的錯誤信息 Smarty既可自動從文件系統(tǒng)加載插件,也可在運行時通過register_* API函數(shù)注冊插件。當然,也可以通過unregister_* API函數(shù)卸載已經(jīng)載入的插件 對于只在運行時注冊的插件函數(shù)不必遵守命名約定 如果某個插件依賴其它插件的某些功能(事實上,一些插件被綁定在Smarty中),那么可以通過如下方法加載需要的插件:
1 2 3 | <?php
require_once $smarty->_get_plugin_filepath( 'function' , 'html_options' );
?>
|
按照慣例,Smarty對象通常作為最后一個參數(shù)傳遞給插件,但有兩個例外:1、調(diào)節(jié)器不須接受Smarty對象的傳遞;2、為了向前兼容舊版Smarty,塊插件將$repeat排在Smarty對象后面作為最后一個參數(shù)($smarty作為倒數(shù)第二個參數(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ù)標簽代替,例如{fetch}函數(shù)。作為另一種選擇,函數(shù)也可以單純地用來做些非輸出內(nèi)容的任務(wù),如{assign}函數(shù)
如果函數(shù)需要分配(俗話說的賦值)一些變量給模板或者使用Smarty提供的一些函數(shù),可以通過$smarty對象實現(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()方式來進行插件注冊,但是由于與PHP代碼混合在一起,不建議使用 registerPlugin() void registerPlugin(string type, string name, mixed callback, bool cacheable, mixed cache_attrs); registerPlugin()方法在腳本中注冊函數(shù)或方法作為插件。其參數(shù)如下: “type”定義插件的類型,其值為下列之一:“function”、“block”、“compiler”和“modifier” “name”定義插件的函數(shù)名 “callback”為定義的php回調(diào)函數(shù),其類型為下列之一: 1、包含函數(shù)名的字符串; 2、格式為(&$object, $method)的數(shù)組,其中,&$object為引用對象,$method為包含方法名的字符串; 3、格式為($class, $method)的數(shù)組,其中,$class為類名,$method為類中的方法。 “cacheable”和“cache_attrs”參數(shù)大多情況下可以省略 1 <?php 2 header('content-type:text/html;charset=utf-8'); 3 require './init.inc.php'; 4 function eightball($params, $smarty){ 5 $answers = array('Yes', 'No','No way','Outlook not so good','Ask again soon','Maybe in your reality'); 6 $result = array_rand($answers); 7 return $answers[$result]; 8 } 9 $smarty -> registerPlugin('function', 'test', 'eightball');10 $smarty -> display('main.tpl');11 ?>12 <!-- main.tpl -->13 Question: Will we ever have time travel?<br>14 Answer: <{test}>. <br>  【調(diào)節(jié)器】 調(diào)節(jié)器是一些簡短的函數(shù),這些函數(shù)被應(yīng)用于顯示模板前作用于一個變量,或者其它情形中。調(diào)節(jié)器可以連接起來(執(zhí)行) mixed smarty_modifier_name($value, $param1);mixed $value;[mixed $param1, ...]; 調(diào)節(jié)器插件的第一個參數(shù)應(yīng)該直接了當?shù)芈暶魈幚硎裁搭愋停梢允亲址?、?shù)組、對象等等這些類型)。其它的參數(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}。換句話說,他們被封閉在一個模板區(qū)域內(nèi),然后對該區(qū)域的內(nèi)容進行操作。塊函數(shù)優(yōu)先于同名的自定義函數(shù),換句話說,不能同時使用自定義函數(shù){func}和塊函數(shù){func} .. {/func}。
默認地,函數(shù)實現(xiàn)會被Smarty調(diào)用兩次:一次是在開始標簽,另一次是在閉合標簽
從Smarty3.1開始打開標簽回調(diào)(函數(shù))的返回值同樣會被顯示
只有塊函數(shù)的開始標簽具有屬性。所有屬性包含在作為關(guān)聯(lián)數(shù)組的$params變量中,經(jīng)由模板傳遞給模板函數(shù)。當處理閉合標簽時,函數(shù)同樣可訪問開始標簽的屬性
$content變量值取決于函數(shù)是被開始標簽調(diào)用還是被閉合標簽調(diào)用。假如是開始標簽,變量值將為NULL,如果是閉合標簽,$content變量值為模板塊的內(nèi)容。請注意這時模板塊已經(jīng)被Smarty處理過,因此所接收到的是模板的輸出而不是模板資源
&$repeat參數(shù)通過引用傳遞給函數(shù)執(zhí)行,并為其提供控制塊顯示多少次的可能性。默認情況下,在首次調(diào)用塊函數(shù)(塊開始標簽)時,&$repeat變量為true,在隨后的所有塊函數(shù)(閉合標簽)調(diào)用中其值始終為false。函數(shù)每次執(zhí)行返回的&$repeat值為true時,{func} .. {/func}之間的內(nèi)容會被求值,同時參數(shù)$content里的新塊內(nèi)容會再次調(diào)用執(zhí)行函數(shù)
如果嵌套了塊函數(shù),可以通過$smarty->_tag_stack變量訪問找出父塊函數(shù)。只須對塊函數(shù)運行一下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
|
模板繼承 繼承帶來了模板面向?qū)ο蟾拍睿╫op),它允許定義一個或多個基模板供子模板繼承。繼承意味著子模板可覆蓋所有或部份父模板中命名相同的塊區(qū)域 模板繼承是一種編譯時進程,其將建立一個獨立的編譯模板文件。與對應(yīng)的基于載入{include}子模板解決方案相比,當解釋模板時,前者有更好的性能 {extends} 繼承 {extends}標簽用在模板繼承中子模版對父模板的繼承 {extends}標簽用在模版中的第一行 如果子模板用{extends}標簽繼承父模板,那么它只能包含{block}標簽(內(nèi)容),其它任何模板內(nèi)容都將忽略 使用此語法為模板資源繼承$template_dir目錄外的文件 {extends file='parent.tpl'}{extends 'parent.tpl'} {* short-hand *} <!-- parent.tpl--><!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Document</title></head><body> 我是父模板中的文字 </body></html><!-- child.tpl--><{extends 'parent.tpl'}> <!-- 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');?> {block} 塊
{block}用來給模板繼承定義一個模板資源的命名區(qū)域。子模板的{block}資源區(qū)域?qū)〈改0逯械南鄳?yīng)區(qū)域。{block}可以嵌套
任意的子、父模板{block}區(qū)域可以彼此結(jié)合??梢酝ㄟ^子{block}定義使用append、prepend選項標記追加或預(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}標簽塊,所有在{block}標簽外的內(nèi)容將被自動移除 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)容不會顯示
|
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' );
?>
|
|