php模板引擎smarty的內建函數之二

黄舟
發布: 2023-03-03 16:50:01
原創
1341 人瀏覽過

Smarty自帶一些內建函數. 
內建函數是模板語言的一部分. 
使用者不能建立名稱和內建函數一樣的自訂函數,也不能修改內建函數.
(#capture函數、config_load 、foreach ,foreachelse、include、include_php等內建函數請參考php模板引擎smarty使用教學專題內建函數之一)
insert 
if,elseif,else
ldelim,rdelim
literal屬性型別是否必須缺省值描述 
name string Yes n/a  插入函數的名稱 
assign string No n/a  此屬性指定一個變數儲存待插入函數輸出 
script string No n/a  插入函數前需要先包含的函數輸出 
script string No n/a  插入函數前需要先包含的函數輸出 
script string No n/a  插入函數前需要先包含的函數輸出 
script string No n/a  插入函數前需要先包含的函數輸出 
script string No n/a  插入函數前需要先包含的函數輸出 
script string No n/a  插入函數前需要先包含的函數輸出 
script string No n/a  插入函數前需要先包含的函數輸出 
script string No n/a php腳本名稱
 

[var ...] [var type] No n/a  傳遞給待插入函數的本地參數

Insert 函數類似欲inluce 函數,不同之處是insert 所包含的內容不會被緩存,每次調用該模板都會重新執行該函數.


例如你在頁面上端使用一個帶有廣告條位置的模板,廣告條可以包含任何HTML、圖象、FLASH等混合信息. 因此這裡不能使用一個靜態的鏈接,同時我們也不希望該廣告條被緩存. 這就需要在insert 函數指定:#banner_location_id# 和#site_id# 值(從配置文件中取),同時需要一個函數取廣告條的內容信息.
 
insert 函數示範 

{* example of fetching a banner *} {insert name="getBanner" lid=#banner_location_id# sid=#site_id#}


在此例中,我們使用了getBanner 作為name 屬性,同時傳遞了# banner_location_id# 和#site_id# 兩個參數. 接下來Smarty 在你的php 程式中搜尋名為insert_getBanner() 的函數,#banner_location_id# 和#site_id# 的值被組合成一個陣列作為函數的第一個參數傳遞給該函數. 為了避免函數命名混亂,所有的insert 函數都必須以insert_ 開頭. 你的insert_getBanner() 函數根據傳遞的參數執行並返回執行的結果. 這些結果就顯示在模板中調用該函數的位置.在此範例中Smarty 呼叫該函數類似insert_getBanner(array("lid"=>"12345","sid"=>67890"));並將傳回的結果顯示在呼叫的位置. 
 
如果設定了assign 屬性,該屬性對應的變數名稱用於保存待包含函數的輸出,這樣待包含函數的輸出就不會直接顯示了.注意:賦給模板變數的輸出資訊在快取的時候同樣無效.
 
如果指定了script 屬性,在調用函數並執行前將先包含(只包含一次)script指定的php 腳本. 這是為了防止被調用的函數不存在,先調用包含該函數的php 腳本將避免該情況.
 
Smart物件作為函數的第二個參數被傳遞,在待包含函數中可以透過$this 存取並修改smarty 物件資訊.
技術要點: 使範本的一部分不被快取. 如果開啟了快取, insert 函數卻不會被緩存,每次調用頁面它們都會被動態加載,即使是在緩存頁面中. 該特性可以廣泛應用於廣告條、投票、實時天氣預報、搜索結果、反饋信息等區域.
 
if,elseif,else

Smarty 中的if 語句和php 中的if 語句一樣靈活易用,並增加了幾個特性以適宜模板引擎. if 必須於/if 成對出現. 可以使用else 和elseif 子句. 可以使用以下條件修飾字:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、 !=、>、=. 使用這些修飾字時必須和變數或常數用空格格開.

語句示範{if $name eq "Fred"} Welcome Sir.{elseif $name eq "Wilma "} Welcome Ma'am.{else} Welcome, whatever you are.{/if}{* an example with "or" logic *}{if $name eq "Fred" or $name eq "Wilma"} ... {/if}{* same as above *}{if $name == "Fred" || $name == "Wilma"} ...{/if}{* the following syntax will NOT work, conditional qualifiers must be separated from surrounding elements by spaces *}{if $name=="Fred" || $name=="Wilma"} ...{/if}{* parenthesis are allowed *}{if ( $amount 1000 ) and $volume >= #minVolAmt#} ...{/if}{* you can also embed php function calls *}{if count($var) gt 0} ...{/if}{* test if values are even or odd *}{if $var is even} ...{/if}{if $var is odd} ...{/if}{if $var is not odd} ...{/ if}{* test if var is divisible by 4 *}{if $var is div by 4} ...{/if}{* test if var is even, grouped by two. i.e.,0=even, 1=even , 2=odd, 3=odd, 4=even, 5=even, etc. *}{if $var is even by 2} ...{/if}{* 0=even, 1=even, 2=even , 3=odd, 4=odd, 5=odd, etc. *}{if $var is even by 3} ...{/if}🎜

ldelim,rdelim
ldelim 和rdelim 用於輸出分隔符,也就是大括號"{" 和"}". 模板引擎總是嘗試解釋大括號內的內容,因此如果需要輸出大括號,請使用此方法.
使用ldelim, rdelim 示範{* this will print literal delimiters out of the template *}{ldelim}funcname{rdelim} is how functions look in Smarty!輸出結果:{funcname} is how functions look in Smarty!

Literal 標籤區域內的資料將被當作文字處理,此時範本將忽略其內部的所有字元資訊. 此特性用於顯示有可能包含大括號等字元資訊的javascript 腳本. 當這些資訊處於{literal} {/literal} 標籤中時,模板引擎將不會分析它們,而直接顯示.

 
literal 標籤示範 
{literal}
 

php 標籤來示範 

{php} // including a php script directly // from the template. include("/path/to/display_weather.php"); {/php}

section_weather.php"); {/php}


section,sectionelse


模板的section 用於遍歷數組中的資料. section 標籤必須成對出現. 必須設定name 和loop 屬性. 名稱可以是包含字母、數字和下劃線的任意組合. 可以嵌套但必須保證嵌套的name 唯一.變數loop (通常是陣列)決定循環執行的次數. 當需要在section 迴圈內輸出變數時,必須在變數後加上中括號包含的name 變數. sectionelse 當loop 變數無值時被執行.

屬性類型是否必須缺少省值描述 

name string Yes n/a  此循環的名稱 
loop [$variable_name] Yes n/a  決定循環次數的變數名稱 
start integer No 0  循環執行的初始位置。如果該值為負數,開始位置從數組的尾部算起. 例如:如果數組中有7個元素,指定start為-2,那麼指向當前數組的索引為5. 非法值(超過了循環數組的下限)將被自動調整為最接近的合法值. 
step integer No 1  該值決定循環的步長. 例如指定step=2將只遍歷下標為0、2、4等的元素. 如果step為負值,那麼遍歷數組的時候從後向前遍歷. 
max integer No 1  設定循環最大執行次數. 
show boolean No true  決定是否顯示該循環.

section 函數示範
{* this example will print out all the values array *}
{section name=customer loop=$custid}
 id: {$custid[customer]}

{/section}

輸出結果:

id: 1000
1000
br>

id: 1002


loop示範

輸出結果:

{* the loop variable only determines the number of times to loop. you can access any variable from the template within the section. Thisex , $name and $address are all arrays containing the same number of values *}{section name=customer loop=$custid} id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}

{/section}輸出結果:id: 1000
name: John Smith
address: 253 N 45th

id: 1001
name: Jack Jones
address: 417 Mulberry ln

id: 1002
name: Jane Munson
address: 5605 apple st


section 名稱示範{* 該部分的名稱可以是任何你喜歡的內容,
 它用於引用該部分中的資料*}
{section name=mydata loop=$custid}
 id: {$custid[mydata ] }

 名稱:{$name[mydata]}

 地址:{$address[mydata]}

 


{/section} 
造型部分演講{ * 部分可嵌套深度。透過嵌套部分,您可以存取複雜的資料結構,例如多維數組。在此範例中,$contact_type[customer] 是目前客戶的聯絡人類型陣列。 *}{部分名稱=客戶循環=$custid} ID:{$custid[客戶]}
姓名:{$name[客戶]}
位址:{$address[客戶]}
{部分名稱=聯絡循環=$contact_type[客戶]}  {$contact_type[客戶][聯絡人]}:{$contact_info[客戶][聯絡人]}
{/section}

{/section} 輸出結果:id: 1000
姓名:John Smith
地址:253 N 45th
家庭電話:555-555-5555
手機:555-555-5555
電子郵件:john@mydomain. com

id:1001
姓名:Jack Jones
網址:417 Mulberry ln
家用電話:555- 555-5555
手機:555-555-5555
電子郵寄:jack@mydomain.com

id:1002
姓名:Jane Munson
地址:5605 apple st
家用電話:555-555-5555
手機:555-555 -5555
電子郵件:jane@mydomain.com

 
section 遍歷多維示範示範{* This is an example of print an associative array
 of data inside a section *}
{section name
 of data inside a section *}
{section name=comer loop=$contacts}
 name: {$contacts[customer].name}
 家庭:{$contacts[customer].home}

 手機:{$contacts[customer].cell}

 電子郵件:{$contacts[customer].email}

{/section}

輸出結果:

name: John Smith

home: 555-555-5555

555-555-5555

e-mail: john @mydomain.com


姓名:Jack Jones

家庭電話:555-555-5555

手機:555-555-5555
電子郵件:jack@mydomain.com
姓名:Jane Munson

家庭電話:555-555-5555

手機:555-555-5555

電子郵件:jane @mydomain.com

 
sectionelse 示範{* 如果沒有$custid 值,sectionelse 會執行*}
{section name=customer Loop=$custid}
 id: {$custid[customer]}

{sectionelse }

 沒有$custid.

{/section}

Section 中的值循環也有呼叫的變數名稱。 透過如下方式呼叫{$smarty.section.sectionname.varname}.
 

strip

Web開發者多次遭遇空格和回車影響HTML輸出的結果(瀏覽器的「特性」),以獲得特定的結果,因此你必須在模板中運行所有標籤。 通常在難以理解或難以處理的模板中會遇到此問題。

Smarty 在顯示前將除掉任何位於 {strip}{/strip} 中資料的首空格尾和回車的區域。 這樣可以保證模板很容易理解,不用擔心省略的空格導致問題。
strip 示範標籤{* the following will be all run into one line On output *}
{strip}


  ;
  
 

 .php1.cn/">    這是一個測試
   
  

{/strip}



輸出結果:

{strip}

 

 


   ;    這是一個測試
   
  
{/strip}

🎜輸出結果:🎜🎜
🎜

請注意上例,所有行都以HTML標籤開頭結尾. 所有行被組織到一起運行. 如果在行首和行尾有文本的話,它們也會被組織到一起,就有可能得到你不想得到的結果.

 以上就是php模板引擎smarty的內建函數之二的內容,更多相關內容請關注PHP中文網(www.php.cn)! 


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!