當前位置:首頁 » Smarty教學 » Smaty section,sectionelse

Smaty section,sectionelse

Smaty section,sectionelse實例代碼教學- Smarty模板的 section 用於遍曆數組中的數據. section 標簽必須成對出現. 必須設置 name 和 loop 屬性.


Table of Contents目錄

indexindex_

previndex_

next

iteration

first

last

row

num

loop

show

total


Attribute Name Type Required Default Description
name string Yes n/a The name of the section
loop [$variable_name] Yes n/a The name of the variable to determine # of loop iterations
start integer No 0 The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.
step integer No 1 The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards.
max integer No 1 Sets the maximum number of times the section will loop.
show boolean No true determines whether or not to show this section

屬性 類型 是否必須 缺省值 描述
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 用於遍曆數組中的數據. section 標簽必須成對出現. 必須設置 name 和 loop 屬性. 名稱可以是包含字母、數字和下劃線的任意組合. 可以嵌套但必須保證嵌套的 name 唯一. 變量 loop (通常是數組)決定循環執行的次數. 當需要在 section 循環內輸出變量時,必須在變量後加上中括號包含著的 name 變量. sectionelse 當 loop 變量無值時被執行.


Smarty手冊範例 7-15. section 函數演示

{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
{/section}

輸出結果:

id: 1000<br>
id: 1001<br>
id: 1002<br>


Smarty手冊範例 7-16.loop 變量演示

{* the loop variable only determines the number of times to loop.
 you can access any variable from the template within the section.
 This example assumes that $custid, $name and $address are all
 arrays containing the same number of values *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
	name: {$name[customer]}<br>
	address: {$address[customer]}<br>
	<p>
{/section}


輸出結果:

id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
<p>


Smarty手冊範例 7-17. section 名稱演示

{* the name of the section can be anything you like,
 and it is used to reference the data within the section *}
{section name=mydata loop=$custid}
	id: {$custid[mydata]}<br>
	name: {$name[mydata]}<br>
	address: {$address[mydata]}<br>
	<p>
{/section}


Smarty手冊範例 7-18. 嵌套 section 演示

{* sections can be nested as deep as you like. With nested sections,
 you can access complex data structures, such as multi-dimensional
 arrays. In this example, $contact_type[customer] is an array of
 contact types for the current customer. *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
	name: {$name[customer]}<br>
	address: {$address[customer]}<br>
	{section name=contact loop=$contact_type[customer]}
		{$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br>
	{/section}
	<p>
{/section}


輸出結果:

id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: john@mydomain.com<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<br>
<p>


Smarty手冊範例 7-19. section 遍曆多維數組演示

{* This is an example of printing an associative array
 of data within a section *}
{section name=customer loop=$contacts}
	name: {$contacts[customer].name}<br>
	home: {$contacts[customer].home}<br>
	cell: {$contacts[customer].cell}<br>
	e-mail: {$contacts[customer].email}<p>
{/section}


輸出結果:

name: John Smith<br>
home: 555-555-5555<br>
cell: 555-555-5555<br>
e-mail: john@mydomain.com<p>
name: Jack Jones<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<p>
name: Jane Munson<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<p>


Smarty手冊範例 7-20. sectionelse 演示

{* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
{sectionelse}
	there are no values in $custid.
{/section}


Section 循環也有可供調用的變量名. 通過如下方式調用{$smarty.section.sectionname.varname}.

注意:Smarty 1.5.0 版中,section 名稱屬性變量的格式由{%sectionname.varname%}變成 {$smarty.section.sectionname.varname},老版本的格式依然支持,但在手冊的例子中隻提供新的格式.