section,sectionelse

Attribute NameTypeRequiredDefaultDescription
namestringYesn/aThe name of the section
loopmixedYesn/aValue to determine the number of loop iterations
startintegerNo0The 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.
stepintegerNo1The 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.
maxintegerNon/aSets the maximum number of times the section will loop.
showbooleanNotruedetermines whether or not to show this section

Template sections are used for looping over arrays of data. All section tags must be paired with /section tags. Required parameters are name and loop. The name of the section can be anything you like, made up of letters, numbers and underscores. Sections can be nested, and the nested section names must be unique from each other. The loop variable (usually an array of values) determines the number of times the section will loop. When printing a variable within a section, the section name must be given next to variable name within brackets []. sectionelse is executed when there are no values in the loop variable.

樣板內的 sections 區塊通常被用來處理陣列資料。所有的 section 必須與 /section 標籤成對,並使用參數 nameloop。在 section 內 name 參數的值可以由英文字母、數字、底線所組成。section 標籤可以是巢 狀結構,但是在同一巢狀結構內的 section 的 name 的值必須是不同的,section 內容變數( 通常是陣列形態的值)會決定 section 會執行的次數。當在 section 內輸出變數的值之後,必 須更改 [] 括弧中的值以取得下一個變數。 sectionelse 的執行時機在當沒有值存在於陣列變數中時。

Example 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}

OUTPUT:

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

Example 7-16. section 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}


OUTPUT:

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>

Example 7-17. section names

{* 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}

Example 7-18. 巢狀 sections

{* 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}


OUTPUT:

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@myexample.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@myexample.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@myexample.com<br>
<p>

Example 7-19. sections 與關聯式陣列

{* 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}


OUTPUT:

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

Example 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}

Sections also have their own variables that handle section properties. These are indicated like so: {$smarty.section.sectionname.varname}

section 也有自己處理 section 屬性的專屬變數,參照時所使用的變數名 稱如:{$smarty.section.sectionname.varname}

Note: As of Smarty 1.5.0, the syntax for section property variables has been changed from {%sectionname.varname%} to {$smarty.section.sectionname.varname}. The old syntax is still supported, but you will only see reference to the new syntax in the manual examples.

提醒: 從 Smarty 1.5.0 開始,參照到 section 屬性變數名稱的文法已經從 {%sectionname.varname%} 變成 {$smarty.section.sectionname.varname}。舊的變數名稱依然 被支援,但是在手冊範例中你只會看到新的變數使用方法。

index

index is used to display the current loop index, starting with zero (or the start attribute if given), and incrementing by one (or by the step attribute if given.)

index 會紀錄迴圈現在的索引,index 從0(或是從被給定的值)開始,每次 增加1(或是被給定的值)。

Technical Note: If the step and start section properties are not modified, then this works the same as the iteration section property, except it starts on 0 instead of 1.

技術提醒: 如果 step 與 start 這兩個 section 屬性都沒有被設定的話,則 section 的運作結果會與 iteration 相同,唯一的差別在於此 section 是從0開始而不是1。

Example 7-21. section 的屬性 index

{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{/section}


	OUTPUT:

	0 id: 1000<br>
	1 id: 1001<br>
	2 id: 1002<br>

index_prev

index_prev is used to display the previous loop index. on the first loop, this is set to -1.

index_prev 會紀錄迴圈前一個索引。在第一個迴圈中,此變數的值被設定成-1。

Example 7-22. section 的屬性 index_prev

{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
	{if $custid[customer.index_prev] ne $custid[customer.index]}
    	The customer id changed<br>
	{/if}
	{/section}


	OUTPUT:

	0 id: 1000<br>
    	The customer id changed<br>
	1 id: 1001<br>
    	The customer id changed<br>
	2 id: 1002<br>
    	The customer id changed<br>

index_next

index_next is used to display the next loop index. On the last loop, this is still one more than the current index (respecting the setting of the step attribute, if given.)

index_next 會紀錄迴圈下一個索引。在最後一次迴圈中,此值會被設定 為比現在的索引多1(如果有設定 step 屬性)。

Example 7-23. section 的屬性 index_next

{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
	{if $custid[customer.index_next] ne $custid[customer.index]}
    	The customer id will change<br>
	{/if}
	{/section}


	OUTPUT:

	0 id: 1000<br>
    	The customer id will change<br>
	1 id: 1001<br>
    	The customer id will change<br>
	2 id: 1002<br>
    	The customer id will change<br>

iteration

iteration is used to display the current loop iteration.

iteration 會紀錄現在的迴圈。

Note: This is not affected by the section properties start, step and max, unlike the index property. Iteration also starts with 1 instead of 0 like index. rownum is an alias to iteration, they work identical.

提醒: iteration 不會被 section 的屬性 start, step 與 max 所影響,iteration 的索引也是從1開始,rownum 是 iteration 的別名,iteration 與 rownum 的功能是一樣的。

Example 7-24. section 的屬性 iteration

{section name=customer loop=$custid start=5 step=2}
	current loop iteration: {$smarty.section.customer.iteration}<br>
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
	{if $custid[customer.index_next] ne $custid[customer.index]}
    	The customer id will change<br>
	{/if}
	{/section}


	OUTPUT:

	current loop iteration: 1
	5 id: 1000<br>
    	The customer id will change<br>
	current loop iteration: 2
	7 id: 1001<br>
    	The customer id will change<br>
	current loop iteration: 3
	9 id: 1002<br>
    	The customer id will change<br>

first

first is set to true if the current section iteration is the first one.

如果 section 的 iteration 在第一個的話 first 的值就會是 true。

Example 7-25. section 的屬性 first

{section name=customer loop=$custid}
	{if $smarty.section.customer.first}
    	<table>
	{/if}

	<tr><td>{$smarty.section.customer.index} id:
        	{$custid[customer]}</td></tr>

	{if $smarty.section.customer.last}
    	</table>
	{/if}
	{/section}


	OUTPUT:

	<table>
	<tr><td>0 id: 1000</td></tr>
	<tr><td>1 id: 1001</td></tr>
	<tr><td>2 id: 1002</td></tr>
	</table>

last

last is set to true if the current section iteration is the last one.

如果 section 的 iteration 在最後一個的話 last 的值就會是 true。

Example 7-26. section 的屬性 last

{section name=customer loop=$custid}
	{if $smarty.section.customer.first}
    	<table>
	{/if}

	<tr><td>{$smarty.section.customer.index} id:
        	{$custid[customer]}</td></tr>

	{if $smarty.section.customer.last}
    	</table>
	{/if}
	{/section}


	OUTPUT:

	<table>
	<tr><td>0 id: 1000</td></tr>
	<tr><td>1 id: 1001</td></tr>
	<tr><td>2 id: 1002</td></tr>
	</table>

rownum

rownum is used to display the current loop iteration, starting with one. It is an alias to iteration, they work identically.

rownum 可顯示現在迴圈的 iteration,rownum 從1開始,rownum 是 iteration 的別名,此兩者的功能是一樣的。

Example 7-27. section 的屬性 rownum

{section name=customer loop=$custid}
	{$smarty.section.customer.rownum} id: {$custid[customer]}<br>
	{/section}


	OUTPUT:

	1 id: 1000<br>
	2 id: 1001<br>
	3 id: 1002<br>

loop

loop is used to display the last index number that this section looped. This can be used inside or after the section.

loop 可以紀錄此次 section 迴圈中最後一個索引數字,可以使用於 section 中間或是 section 之後。

Example 7-28. section 的屬性 index

{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{/section}

	There were {$smarty.section.customer.loop} customers shown above.

	OUTPUT:

	0 id: 1000<br>
	1 id: 1001<br>
	2 id: 1002<br>

	There were 3 customers shown above.

show

show is used as a parameter to section. show is a boolean value, true or false. If false, the section will not be displayed. If there is a sectionelse present, that will be alternately displayed.

show 是 section 的參數。 show 的值是 boolean 型態的 true 與 false。如果值是 false 的話,則 section 的內容將不會 被展示出來,如果有 sectionelse 標籤的話,則 sectionelse 的內容將會被顯示出來。

Example 7-29. section 的屬性 show

{* $show_customer_info may have been passed from the PHP
	application, to regulate whether or not this section shows *}
	{section name=customer loop=$custid show=$show_customer_info}
	{$smarty.section.customer.rownum} id: {$custid[customer]}<br>
	{/section}

	{if $smarty.section.customer.show}
	the section was shown.
	{else}
	the section was not shown.
	{/if}


	OUTPUT:

	1 id: 1000<br>
	2 id: 1001<br>
	3 id: 1002<br>

	the section was shown.

total

total is used to display the number of iterations that this section will loop. This can be used inside or after the section.

total 會紀錄此 section 中 iteration 的迴圈數目,可以用在 section 中 間或是 section 之後。

Example 7-30. section 的屬性 total

{section name=customer loop=$custid step=2}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{/section}

	There were {$smarty.section.customer.total} customers shown above.

	OUTPUT:

	0 id: 1000<br>
	2 id: 1001<br>
	4 id: 1002<br>

	There were 3 customers shown above.