版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
Example5-2.capitalize
例5-2.首字大寫
index,php:
$smarty=newSmarty;
$smarty->assign(,articleTitle,,'Policebegincampaigntorundown
jaywalkers.J;
$smarty->display(Jindex,tpl');
index,tpl:
{SarticleTitle}
{$articleTitle|capitalize)
OUTPUT:
Policebegincampaigntorundownjaywalkers.
PoliceBeginCampaignToRundownJaywalkers.
Example5-3.count_characters
index,php:
$smarty=newSmarty;
$smarty->assign(JarticleTitle,,'ColdWaveLinkedtoTemperatures.J;
$smarty-〉display('index.tpT);
index,tpl:
{$articleTitle)
{$articleTitle|count_characters}
{$articleTitle|count_characters:true}〃決定是否計算空格
OUTPUT輸出:
ColdWaveLinkedtoTemperatures.
29
33
Example5-4.cat將cat里的值連接到給定的變量后面.
index.php:
$smarty=newSmarty;
$smarty->assign(JarticleTitle,,“Psychicspredictworlddidn,tend");
$smarty->display(Jindex.tpF);
index,tpl:
{$articleTitle|cat:yesterday.
OUTPUT:
Psychicspredictworlddidn,tendyesterday.
Example5-5.count_paragraphs計算變量里的段落數量。
index,php:
$smarty=newSmarty;
$smarty->assign(,articleTitleJ,“WarDimsHopeforPeace.Child,sDeath
RuinCouple'sHoliday.\n\nManisFatallySlain.DeathCausesLoneliness,
FeelingofIsolation.,z);
$smarty-〉display('index.tpT);
index,tpl:
{$articleTitle)
{$articleTitle|countparagraphs}
OUTPUT:
WarDimsHopeforPeace.Child,sDeathRuinsCouple,sHoliday.
ManisFatallySlain.DeathCausesLoneliness,FeelingofIsolation.
2
Example5-6.count_sentences計算變量里句子的數量
index.php:
$smarty=newSmarty;
$smarty->assign(,articleTitle,,'TwoSovietShipsCollide-OneDies.
EnragedCowInjuresFarmerwithAxe.');
$smarty->display(Jindex.tpF);
index,tpl:
{$articleTitle)
{SarticleTitle|count_sentences)
OUTPUT:
TwoSovietShipsCollide-OneDies.EnragedCowInjuresFarmerwithAxe.
2
Example5-7.count_words計算變量里的詞數
index,php:
$smarty=newSmarty;
$smarty->assignarticleTitle,,'DealersWillHearCarTalkatNoon.');
$smarty->display('index.tpF);
index,tpl:
{$articleTitle}
{$articleTitle|count_words)
OUTPUT:
DealersWillHearCarTalkatNoon.
7
Example5-10.default為空變量設置一個默認值。
當變量為空或者未分配的時候,將由給定的默認值替代輸出。
index.php:
$smarty=newSmarty;
$smarty->assignarticleTitle,,'DealersWillHearCarTalkatNoon.');
$smarty->display('index.tpT);
index.tpl:
{$articleTitle|default:"notitle”}
{$myTitle|default:,znotitle”}
OUTPUT:
DealersWillHearCarTalkatNoon.
notitle
Example5-11.escape用于html轉碼,url轉碼,在沒有轉碼的變量上轉換單引號,十六進
制轉碼,十六進制美化,或者javascript轉碼。默認是html轉碼。
index,php:
$smarty=newSmarty;
$smarty->assignarticleTitle,,“'StiffOppositionExpectedto
CasketlessFuneralPlan,〃);
$smarty->displayindex.tpT);
index.tpl:
{$articleTitle)
{$articleTitle|escape)
{$articleTitle|escape://html,3{*escapes&<>*}
{$articleTitle|escape://htmlair,}{*escapesALLhtmlentities*}
{$articleTitle|escape:〃url〃}
{$articleTitle|escape:"quotes”}
<a
href="mailto:{$EmailAddress|escape:"hex"}〃>{$EmailAddress|escape:/zhe
xentity"}</a>
OUTPUT:
'StiffOppositionExpectedtoCasketlessFuneralPlan'
'StiffOppositionExpectedtoCasketlessFuneralPlan'
'StiffOppositionExpectedtoCasketlessFuneralPlan'
'StiffOppositionExpectedtoCasketlessFuneralPlan'
%27Stiff+0pposition+Expected+to+Casketless+Funeral+Plan%27
\'StiffOppositionExpectedtoCasketlessFuneralPlan\,
<a
Example5-14.nl2br
例5-14.換行符替換成<br/>
index,php:
$smarty=newSmarty;
$smarty->assign(,articleTitle,,“Sunorrainexpected\ntoday,darkto
$smarty->displayCindex,tpl');
index,tpl:
{$articleTitle|nl2br}
OUTPUT:
Sunorrainexpected<br/>today,darktonight
Example5-15.regex_replace
例5-15.正則替換
index,php:
Ssmarty=newSmarty;
$smarty->assign('articleTitle,,^Infertilityunlikelyto\nbepassedon,
expertssay.〃);
$smarty->displayCindex.tpT);
index,tpl:
{*replaceeachcarriagereturn,tab&newlinewithaspace*}
{*使用空格替換每個回車,tab,和換行符*}
{$articleTitle)
{SarticleTitle|regexjeplace:〃/[\r\t\n]/〃:〃〃}
OUTPUT:
Infertilityunlikelyto
bepassedon,expertssay.
Infertilityunlikelytobepassedon,expertssay.
Example5-16.replace
例5-16.替換
index,php:
Ssmarty=newSmarty;
$smarty->assign(,articleTitle,,“Child'sStoolGreatforUsein
Garden.z,);
$smarty->displayCindex.tpF);
index,tpl:
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard”}
{$articleTitle|replace:z,〃:〃“}
OUTPUT:
Child,sStoolGreatforUseinGarden.
Child,sStoolGreatforUseinVineyard.
Child,sStoolGreatforUseinGarden.
Example5-17.spacify
例5-17.插空插空(不知道這個詞是什么意思,顧名思義了八八)是一種在字符串的每個字符
之間插入空格或者其他的字符(串).
index,php:
$smarty=newSmarty;
$smarty->assignarticleTitle,,JSomethingWentWronginJetCrash,
ExpertsSay.');
$smarty->displayCindex.tpT);
index.tpl:
{$articleTitle)
{SarticleTitle|spacify}
{$articleTitle|spacify:〃八
OUTPUT:
SomethingWentWronginJetCrash,ExpertsSay.
SomethingWentWronginJetCrash,Expert
sSay.
S-m一晨「e一出入“-w八,-o“n-一「「
JetCrash,ExpertsSay.
Example5-18.string_format
字符串格式化是?種格式化字符串的方法.例如格式化為十進制數等等.使用sprintf語法格
式化
index,php:
$smarty=newSmarty;
$smarty->assign(Jnumber,,23.5787446);
$smarty->displayCindex,tpl');
index,tpl:
{$number}
{$number|string_format:〃%.2f〃}
{$number|stringformat:〃%d〃}
OUTPUT:
23.5787446
23.58
24
Example5-19.strip用一個空格或一個給定字符替換所有重復空格,換行和制
表符.注意:如果想要去除模板文本中的區(qū)塊,請使用strip函數
例5-19,去除(多余空格)
index,php:
$smarty=newSmarty;
$smarty->assign(,articleTitle,,''Grandmotherof\neightmakes\tholein
one.八);
$smarty->displayCindex.tpl');
index,tpl:
{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:〃 〃}
OUTPUT:
Grandmotherof
eightmakesholeinone.
Grandmotherofeightmakesholeinone.
Grandmother
Example5-20.strip_tags去除v和〉標簽,包括在〈和〉之間的任何內容.
例5-20,去除Html標套
index,php:
$smarty=newSmarty;
$smarty->assign(?articleTitleJ,“BlindWomanGets<font
face=\z,helvetica\z/>NewKidney</font>fromDadsheHasn,tSeenin
<b>years</b>.〃);
$smarty->displayCindex,tpl');
index.tpl:
{$articleTitle)
{$articleTitle|strip_tags)
OUTPUT:
BlindWomanGets<fontface二〃helvetica〃〉NewKidney</font>fromDadshe
Hasn,tSeenin<b>years</b>.
BlindWomanGetsNewKidneyfromDadsheHasn'tSeeninyears.
Example5-21.truncate
例5-21.截取從字符串開始處截取某長度的字符.默認是80個.
你也可以指定第二個參數作為追加在截取字符串后面的文本字串.該追加字串被計算在截取
長度中。
默認情況下,smarty會截取到一個詞的末尾。
如果你想要精確的截取多少個字符,把第三個參數改為“true”
index,php:
$smarty=newSmarty;
$smarty->assignarticleTitle,,'TwoSistersReuniteafterEighteen
YearsatCheckoutCounter.');
$smarty->disp1ay(yindex.tpT);
index,tpl:
{$articleTitle)
{$articleTitle|truncate)
{$articleTitle|truncate:30}
{$articleTitle|truncate:30
{$articleTitle|truncate:30:,z〃}
{$articleTitle|truncate:30:〃〃:true)
{$articleTitle|truncate:30:〃???〃:true)
OUTPUT:
TwoSistersReuniteafterEighteenYearsatCheckoutCounter.
TwoSistersReuniteafterEighteenYearsatCheckoutCounter.
TwoSistersReuniteafter...
TwoSistersReuniteafter
TwoSistersReuniteafter—
TwoSistersReuniteafterEigh
Two
Example5-22.upper
例5-22.大寫將變量改為大寫
index,php:
$smarty=newSmarty;
$smarty->assignCarticleTitle,,“IfStrikeisn'tSettledQuicklyitmay
LastaWhile.〃);
$smarty->displayCindex,tpl');
index.tpl:
{$articleTitle)
{$articleTitle|upper}
OUTPUT:
IfStrikeisn,tSettledQuicklyitmayLastaWhile.
IFSTRIKEISN'TSETTLEDQUICKLYITMAYLASTAWHILE.
Example5-23.wordwrap
例5-23,行寬約束可以指定段落的寬度(也就是多少個字符?行,超過這個字符數換行).默
認80.
第二個參數可選,可以指定在約束點使用什么字符(默認是換行符\n).
默認情況下smarly將截取到詞尾,如果想精確到設定長度的字符,請將第三個參數設為ture
index,php:
$smarty=newSmarty;
$smarty->assign(,articleTitle,,“Blindwomangetsnewkidneyfromdad
shehasn'tseeninyears.,z);
$smarty->display(,index.tpF);
index,tpl:
{$articleTitle}
{$articleTitle|wordwrap:30)
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:“<br>\n〃}
{$articleTitle〔wordwrap:30:〃\n〃:true)
OUTPUT:
Blindwomangetsnewkidneyfromdadshehasn,tseeninyears.
Blindwomangetsnewkidney
fromdadshehasn,tseenin
years.
Blindwomangetsnew
kidneyfromdadshe
hasn,tseenin
years.
Blindwomangetsnewkidney<br>
fromdadshehasn,tseeninyears.
Blindwomangetsnewkidneyfr
omdadshehasn,tseeninyear
s.
Ebiningmodifiers
例6-1.組合修改器對于同一個變量,你可以使用多個修改器。它們將從左到右按照設定
好的順序被依次組合使用。使用時必須要用用字符作為它們之間的分隔符。
index,php:
$smarty=newSmarty;
$smarty->assignCarticleTitle5,'SmokersareProductive,butDeathCuts
Efficiency.J;
$smarty->display(Jindex.tpF);
index.tpl:
{$articleTitle)
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:301spacify}
{$articleTitle|lower|spacifyjtruncate:30:,z...〃}
OUTPUT:
SmokersareProductive,butDeathCutsEfficiency.
SMOKERSAREPRODUCTIVE,BUTDEATHCUTSEF
FICIENCY.
smokersareproductive,butdeathcuts...
smokersareproductive,but...
smokersarep...
Chapter7.Built-inFunctions[第七章.內建
函數]
capture函數的作用是捕獲模板輸出的數據并將其存儲到一個變量里,而不是把它們輸出到頁
面.
任何在{capturename="foo"}和{/capture}之間的數據將被存儲到變量$foo中,該變量由name
屬性指定.
在模板中通過$smarty.capture.foo訪問該變量.
如果沒有指定name屬性,函數默認將使用“default”作為參數.
{capture)必須成對出現,即以{/capture}作為結尾,該函數不能嵌套使用.
Example7T.capturingtemplatecontent
例7-1.捕獲模板內容
{*wedon'twanttoprintatablerowunlesscontentisdisplayed*}
{*該例在捕獲到內容后輸出一行包含數據的表格,如果沒有捕獲到就什么也不
輸出*}
{capturename=banner}
{includefile=,,get_banner.tplz/}
{/capture)
{if$smarty.capture,bannerne〃〃}
<tr>
<td>
{$smarty.capture,banner)
</td>
</tr>
{/if}
Example7-2.functionconfig_load
例7-2.演示config_load函數
{configloadfile="colors.conf“}
<html>
<title>{#pageTitle#}</title>
<bodybgcolor=〃{ttbodyBgColor#}〃>
<tableborder=z/{fttableBorderSize#}z,bgcolor=,z{StableBgColor#},z>
<trbgcolor=〃{ftrowBgColor#}〃>
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html
Example7-3.functionconfig_loadwithsection
例7-3.帶section屬性的config_load函數演示
{config_loadfile="colors,conf"sectioniCustomer”}
<html>
<title>{SpageTitle#}</title>
<bodybgcolor=〃{#bodyBgColor#}〃>
<tableborder=”{枇ableBorderSize#}〃bgcolor=z,{#tableBgColor#},z>
<trbgcolor=z,{#rowBgColor#}z,>
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
foreach是除section之外處理循環(huán)的另一種方案(根據不同需要選擇不同的方案).
foreach用于處理簡單數組(數組中的元素的類型一致),它的格式比section簡單許多,缺點
是只能處理簡單數組.
foreach必須和/foreach成對使用,且必須指定from和item屬性.
name屬性可以任意指定(字母、數字和下劃線的組合).
foreach可以嵌套,但必須保證嵌套中的foreach名稱唯一.
from屬性(通常是數組)決定循環(huán)的次數.
foreachelse語句在from變量沒有值的時候被執(zhí)行.
Example7-4.foreach
例7-4.foreach演示
{*thisexamplewillprintoutallthevaluesofthe$custidarray*}
{*該例將輸出數組$custid中的所有元素的值*}
{foreachfrom=$custiditem=currid)
id:{$curr_id}<br>
{/foreach)
OUTPUT:
id:1000<br>
id:1001<br>
id:1002<br>
Example7-5.foreachkey
例7-5.foreach鍵的演示
{*Thekeycontainsthekeyforeachloopedvalue
assignmentlookslikethis:
$smarty-〉assign("contacts”,array(array("phone'=>〃1","fax"=>〃2〃,
&11〃=>〃3〃),
array("phone"=>〃555-4444〃,"fax"=>〃555-3333〃,"cell"=>
〃760-1234〃)));
*}
{*鍵就是數組的下標,請參看關于數組的解釋*}
{foreachname=outeritem=contactfrom=$contacts}
{foreachkey=keyitem=itemfrom=$contact}
{$key}:<br>
{/foreach)
{/foreach}
OUTPUT:
phone:l<br>
fax:2<br>
cell:3<br>
phone:555-4444<br>
fax:555-3333<br>
cell:760-1234<br>
Include標簽用于在當前模板中包含其它模板.當前模板中的變量在被包含的
模板中可用.必須指定file屬性,該屬性指明模板資源的位置.
如果設置了assign屬性,該屬性對應的變量名用于保存待包含模板的輸出,這
樣待包含模板的輸出就不會直接顯示了。
Example7-6.functioninclude
例7-6.include函數演示
{includefile="header.tpl〃}
{*bodyoftemplategoeshere*}
{includefile="footer.tpl〃}
可以在屬性中傳遞參數給待包含模板.傳遞給待包含模板的參數只在待包含模
板中可見.如果傳遞的參數在待包含模板中有同名變量,那么該變量被傳遞的參
數替代.
Example7-7.functionincludepassingvariables
例7-7.帶傳遞參數的include函數演示
{includefile="header.tpl〃title二〃MainMenu"tablebgcolor=,z#cOcOcOz/}
{*bodyoftemplategoeshere*}
{includefile二〃footer。tpl〃logo=z/http://my.domain,com/logo.gif,z)
包含$template_dir文件夾之外的模板請使用模板資源說明的格式.
Example7-8.functionincludetemplateresourceexamples
例7-8.使用外部模板資源的include函數演示
{*absolutefilepath*}
{includefile=zz/usr/local/include/templates/header.tpl〃}
{*absolutefilepath(samething)*}
{includefile=zzfile:/usr/local/include/templates/header.tpl〃}
{*windowsabsolutefilepath(MUSTuse〃file:〃prefix)*}
{includefile=z/file:C:/www/pub/templates/header.tpl〃}
{*includefromtemplateresourcenamed"db"*}
{includefile="db:header.tpl”}
inluce_php函數用于在模板中包含php腳本.如果設置了安全模式,被包含的
腳本必須位于$trusted_dir路徑下.include_php函數必須設置file屬性,
該屬性指明被包含php文件的路徑,可以是$trusted_dir的相對路徑,也可
以是絕對路徑.
include_php是解決模板部件化的好方法,它使得php代碼從模板文件中被分
離出來.舉個例子:假設有一個從數據庫中動態(tài)取出數據用于顯示站點導航的模
板,你可以將得數據內容的Php邏輯部分分離出來保存在一個單獨的文件夾下,
并在模板開始的位置包含該php腳本.那么就可以在任何地方包含此模板而不
用擔心之前數據庫信息是否已被程序取出.
即使是在模板中多次地調用php文件,默認情況下它們只被包含一次.你可以
設置once屬性從而指明每次調用都重新包含該文件.如果將once屬性設置
為false,每次調用該文件都將被重新包含.
如果設置了assign屬性,該屬性對應的變量名用于保存待包含php的輸出,
這樣待包含Php文件的輸出就不會直接顯示了。
在待包含php文件中可以通過$this訪問smarty對象.
Example7-9.functioninclude_php
例7-9.includejphp函數演示
load_nav.php
<?php
//loadinvariablesfromamysqldbandassignthemtothe
template
//從mysql數據庫中取得數據,將數據賦給模板變量
require_once("MySQL,class,php");
$sql=newMySQL;
$sql->query("select*fromsite_nav_sectionsorderby
name”,SQL_ALL);
$this->assignCsections',$sql->record);
?>
index,tpl
{*absolutepath,orrelativeto$trusted_dir*}
{*絕對路徑或$trusteddir的相對路徑*)
{include_phpfile=///path/to/load_nav.php”}
{foreachitem="curr_section"from=$sections)
<ahref=,/{$curr_section.url}z/>{$curr_}</a><br>
{/foreach}
例如你在頁面上端使用一個帶有廣告條位置的模板,廣告條可以包含任何HTML、
圖象、FLASH等混合信息.因此這里不能使用一個靜態(tài)的鏈接,同時我們也不希
望該廣告條被緩存.這就需要在insert函數指定:#banner_location_id#和
#site_id#值(從配置文件中取),同時需要一個函數取廣告條的內容信道.
Example7-10.functioninsert
例7-10.insert函數演示
{*exampleoffetchingabanner*}
{insertname="gelBanner"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()函數根據傳遞的參數執(zhí)行并返回執(zhí)行的結果.這些結果就
顯示在覆板中調用該函數的位置.在此例中Smarty調用該函數類似
insert_getBanner(array("lid"=>"12345","sid"=>67890"));并將返回的結果
顯示在癡用的位置.
如果設置了assign屬性,該屬性對應的變量名用于保存待包含函數的輸出,這
樣待包含函數的輸出就不會直接顯示了.注意:賦給模板變量的輸出信息在緩存
的時候同樣無效.
如果指定了script屬性,在調用函數并執(zhí)行前將先包含(只包含一次)script
指定的php腳本.這是為了防止被調用的函數不存在,先調用包含該函數的
Php腳本將避免該情況.
Smart對象作為函數的第二個參數被傳遞,在待包含函數中可以通過$this訪
問并修改smarty對象信息.
技術要點:使模板的一部分不被緩存.如果打開了緩在,insert函數卻不會被
緩存,每次調用頁面它們都會被動態(tài)加載,即使是在緩存頁面中.該特性可以廣
泛應用于廣告條、投票、實時天氣預報、搜索結果、反饋信息等區(qū)域.
Smarty中的if語句和php中的if語句一樣靈活易用,并增加了幾個特性以
適宜模板引擎.if必須于/if成對出現.可以使用else和elseif子句.
可以使用以下條件修飾詞:eq、ne>neq>gt>It、lte>le>gte>ge>iseven>
isodd、isnoteven>isnotodd、not、mod、divby>evenby>oddby>==>!=>>、
<、<=、>=.使用這些修飾詞時必須和變量或常量用空格格開.
Example7-11.ifstatements
例7-11.if語句演示
{if$nameeq"Fred"}
WelcomeSir.
{elseif$nameeq〃Wilma〃}
WelcomeMa'am.
{else}
Welcome,whateveryouare.
{/if}
{*anexamplewith〃or〃logic*}
{if$nameeq〃Fred〃or$nameeq
{/if}
{*sameasabove*}
{if$name==〃Fred〃||$name==〃Wilma〃}
{/if}
{*thefollowingsyntaxwillNOTwork,conditionalqualifiers
mustbeseparatedfromsurroundingelementsbyspaces*}
{if$name==//Fredz,||$name==",Wilma/,}
{/if}
{*parenthesisareallowed*}
{if($amount<0or$amount>1000)and$volume>=SminVolAmt#}
{/if}
{*youcanalsoembedphpfunctioncalls*}
{ifcount($var)gt0}
{/if}
{*testifvaluesareevenorodd*}
{if$variseven)
{/if}
{if$varisodd)
{/if}
{if$varisnotodd)
{/if}
{*testifvarisdivisibleby4*}
{if$varisdivby4}
{/if}
{*testifvariseven,groupedbytwo.i.e.,
0=even,1二even,2=odd,3=odd,4=even,5=even,etc.*}
{if$varisevenby2}
{/if}
{*0=even,l=even,2=even,3=odd,4=odd,5=odd,etc.*}
{if$varisevenby3)
{/if}
Idelim和rdelim用于輸出分隔符,也就是大括號〃{〃和〃}〃.模板引擎總是
嘗試解釋大括號內的內容,因此如果需要輸出大括號,請使用此方法.
Example7-12.Idelim,rdelim
例7T2.使用Idelim,rdelim演示
{*thiswillprintliteraldelimitersoutofthetemplate*}
{Idelim}funcname{rdelim}ishowfunctionslookinSmarty!
OUTPUT:
{funcname}ishowfunctionslookinSmarty!
Literal標簽區(qū)域內的數據將被當作文本處理,此時模板將忽略其內部的所有字
符信息.該特性用于顯示有可能包含大括號等字符信息的javascript腳本.
當這些信息處于{literal}{/literal}標簽中時,模板引擎將不分析它們,而
直接顯示.
Example7-13.literaltags
例7-13.literal標簽演示
{literal}
<scriptlanguage二javascript〉
<!一
functionisblank(field){
if(field,value=='')
{returnfalse;}
else
(
document,loginform,submit();
returntrue;
)
)
//-->
</script>
{/literal}
php標簽允許在模板中直接嵌入php腳本.是否處理這些語句取決于
$phphandling的設置.該語句通常不需要使用,當然如果你非常了解此特性或
認為必須要用,也可以使用.
Example7-14.phptags
例7-14.php標簽演示
{php}
//includingaphpscriptdirectly
//fromthetemplate.
includeC/path/to/display_weather.php〃);
{/php}
模板的section用于遍歷數組中的數據.section標簽必須成對出現.必須設
置瓶相和/oo夕屬性.名稱可以是包含字母、數字和下劃線的任意組合.可以
嵌套但必須保證嵌套的name唯一.變量loop(通常是數組)決定循環(huán)執(zhí)行的
次數.當需要在section循環(huán)內輸出變量時,必須在變色后加上中括號包含著
的name變量.sectionelse當loop變量無值時被執(zhí)行.
Example7-15.section
例7-15.section函數演示
{*thisexamplewillprintoutallthevaluesoftheScustidarray*}
{sectionname=customerloop=$custid)
id:{$custid[customer]}<br>
{/section)
OUTPUT:
id:1000<br>
id:1001<br>
id:1002<br>
Example7-16.sectionloopvariable
例7T6.loop變量演示
{*theloopvariableonlydeterminesthenumberoftimestoloop,
youcanaccessanyvariablefromthetemplatewithinthesection.
Thisexampleassumesthat$custid,$nameand$addressareall
arrayscontainingthesamenumberofvalues*}
{sectionname=customerloop=$custid)
id:{$custid[customer]}<br>
name:{$name[customer]}<br>
address:{$address[customer]}<br>
<P>
{/section)
OUTPUT:
id:1000<br>
name:JohnSmith<br>
address:253N45th<br>
<P>
id:1001<br>
name:JackJones<br>
address:417Mulberryln<br>
<P>
id:1002<br>
name:JaneMunson<br>
address:5605applest<br>
<p>
Example7-17.sectionnames
例7T7.section名稱演示
{*thenameofthesectioncanbeanythingyoulike,
anditisusedtoreferencethedatawithinthesection*}
{sectionname=mydataloop=$custid)
id:{$custid[mydata])<br>
name:{$name[mydata]}<br>
address:{$address[mydata]}<br>
<P>
{/section)
Example7-18.nestedsections
例7T8.嵌套section演示
{*sectionscanbenestedasdeepasyoulike.Withnestedsections,
youcanaccesscomplexdatastructures,suchasmulti-dimensional
arrays.Inthisexample,$contact_type[customer]isanarrayof
contacttypesforthecurrentcustomer.*}
{sectionname=customerloop=$custid)
id:{$custid[customer]}<br>
name:{$name[customer]}<br>
address:{$address[customer]}<br>
{sectionname=contactloop=$contact_type[customer])
{$contact_type[customer][contact]}:
{$contactinfo[customer][contact])<br>
{/section)
<P>
{/section)
OUTPUT:
id:1000<br>
name:JohnSmith<br>
address:253N45th<br>
homephone:555-555-5555<br>
cellphone:555-555-5555<br>
e-mail:john@<br>
<P>
id:1001<br>
name:JackJones<br>
address:417Mulberryln<br>
homephone:555-555-5555<br>
cellphone:555-555-5555<br>
e-mail:jack@<br>
<P>
id:1002<br>
name:JaneMunson<br>
address:5605applest<br>
homephone:555-555-5555<br>
cellphone:555-555-5555<br>
e-mail:jane@<br>
<P>
Example7-19.sectionsandassociativearrays
例7T9.section遍歷多維數組演示
{*Thisisanexampleofprintinganassociativearray
ofdatawithinasection*}
{sectionname=customerloop=$contacts)
name:{$contacts[customer].name}<br>
home:{$contacts[customer],home)<br>
cell:{$contacts[customer].cell}<br>
e-mail:{$contacts[customer].email)<p>
{/section)
OUTPUT:
name:JohnSmith<br>
home:555-555-5555<br>
cell:555-555-5555<br>
e-mail:john@<p>
name:JackJones<br>
homephone:555-555—5555<br>
cellphone:555-555-5555<br>
e-mai1:jack@<p>
name:JaneMunson<br>
homephone:555-555—5555<br>
cellphone:555-555-5555<br>
e-mail:jane@<p>
Example7-20.sectionelse
例7-20.sectionelse演示
{*sectionelsewillexecuteifthereareno$custidvalues*}
{sectionname=customerloop=$custid}
id:{$custid[customer]}<br>
{sectionelse)
therearenovaluesinScustid.
{/section)
Section循環(huán)也有可供調用的變量名.通過如下方式調用
{$smarty.section,sectionname,varname).
注意:Smarty1.5.0版中,section名稱屬性變量的格式由
{%sectionname.varname%}變成{$smarty.section,sectionname,varname},老
版本的格式依然支持,但在手冊的例子中只提供新的格式.
Example7-21.sectionpropertyindex
例7-21.section的index屬性演示
index用于顯示當前循環(huán)的索引,從0開始(如果指定了start屬性,那么由該
值開始),每次加1(如果指定了step屬性,那么由該值決定).
技術細節(jié):如果沒有指定step和start屬性,此值的作用和iteration類似,
只不過從0開始而已.
{sectionname=customerloop=$custid}
{$smarty.section,customer,index}id:{$custid[customer]}<br>
{/section}
OUTPUT:
0id:1000<br>
1id:1001<br>
2id:1002<br>
Example7-22.sectionpropertyindex_prev
例7-22.section的index_prev屬性演示index_prev用于顯示上一個循環(huán)
索引值.循環(huán)開始時,此值為-1.
{sectionname=customerloop=$custid)
{$smarty.section,customer,index}id:{$custid[customer]}<br>
{*FYI,$custid[customer,index]and$custid[customer]are
identicalinmeaning*}
{if$custid[customer,indexprev]ne$custid[customer,index]}
Thecustomeridchanged<br>
{/if}
{/section}
OUTPUT:
0id:1000<br>
Thecustomeridchanged<br>
1id:1001<br>
Thecustomeridchanged<br>
2id:1002<br>
Thecustomeridchanged<br>
Example7-23.sectionpropertyindex_next
例7-22.section的index_next屬性演示index_next用于顯示下一個循環(huán)
索引值.循環(huán)執(zhí)行到最后一正時,此值仍然比當前索引值大1(如果指定了step,
取決于此值).
{sectionname=customerloop=$custid)
{$smarty.section,customer,index}id:{$custid[customer]}<br>
{*FYI,$custid[customer,index]and$custid[customer]are
identicalinmeaning*}
{if$custid[customer.index_next]ne$custid[customer,index]}
Thecustomeridwillchange<br>
{/if}
{/section}
OUTPUT:
0id:1000<br>
Thecustomeridwillchange<br>
1id:1001<br>
Thecustomeridwillchange<br>
2id:1002<br>
Thecustomeridwillchange<br>
Example7-24.sectionpropertyiteration
例7-24.section的iteration屬性演示iteration用于顯示循環(huán)的次數.
注意:iteration不像index屬性受start、step和max屬性的影響,該值總是
從1開始(index是從。開始的).rownum是iteration的別名,兩者等同.
{sectionname=customerloop=$custidstart=5step=2)
currentloopiteration:
{$smarty.section,customer,iteration}<br>
{$smarty.section,customer,index}id:{$custid[customer]}<br>
{*FYI,$custid[customer,index]and$custid[customer]are
identicalinmeaning*}
{if$custid[customer.index_next]ne$custid[customer,index]}
Thecustomeridwillchange<br>
{/if)
{/section}
OUTPUT:
currentloopiteration:1
5id:1000<br>
Thecustomeridwillchange<br>
currentloopiteration:2
7id:1001<br>
Thecustomeridwillchange<br>
currentloopiteration:3
9id:1002<br>
Thecustomeridwillchange<br>
如果當前循環(huán)第一次執(zhí)行,first被設置為true.
Example7-25.sectionpropertyfirst
例7-25.section的first屬性演示
{sectionname=customerloop=$custid)
{if
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 衛(wèi)生紙培訓考核制度
- 重性精神病培訓制度
- 新入職保安員培訓制度
- 學校培訓課桌定制制度
- 學校信息化培訓團隊制度
- 培訓班護學崗制度
- 紋繡培訓學校規(guī)章制度
- 小學防欺凌教育培訓制度
- 企業(yè)管理法律培訓制度
- 醫(yī)生輪崗行政培訓制度
- DB3210T1036-2019 補充耕地快速培肥技術規(guī)程
- 混動能量管理與電池熱管理的協(xié)同優(yōu)化-洞察闡釋
- T-CPI 11029-2024 核桃殼濾料標準規(guī)范
- 統(tǒng)編版語文三年級下冊整本書閱讀《中國古代寓言》推進課公開課一等獎創(chuàng)新教學設計
- 《顧客感知價值對綠色酒店消費意愿的影響實證研究-以三亞S酒店為例(附問卷)15000字(論文)》
- 勞動仲裁申請書電子版模板
- 趙然尊:胸痛中心時鐘統(tǒng)一、時間節(jié)點定義與時間管理
- 家用燃氣灶結構、工作原理、配件介紹、常見故障處理
- ZD(J)9-型電動轉轍機
- DB21T 3414-2021 遼寧省防汛物資儲備定額編制規(guī)程
- 2024年度中國LCOS行業(yè)研究報告:廣泛應用于投影、AR/VR、車載HUD的微顯示技術
評論
0/150
提交評論