section,sectionelse
-
Table of Contents目录
-
index
-
index_prev
-
index_next
-
iteration
-
first
-
last
-
rownum
-
loop
-
show
-
total
Template sections are used for looping over arrays of data. Allsectiontags must be paired with/sectiontags. Required parameters arenameandloop. 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 [].sectionelseis executed when there are no values in the loop variable.
模板的 section 用于遍历数组中的数据.section标签必须成对出现. 必须设置name和loop属性. 名称可以是包含字母、数字和下划线的任意组合. 可以嵌套但必须保证嵌套的 name 唯一. 变量 loop (通常是数组)决定循环执行的次数. 当需要在 section 循环内输出变量时,必须在变量后加上中括号包含着的 name 变量.sectionelse当 loop 变量无值时被执行.
Example 7-15. section 例 7-15. section 函数演示
{* this example will print out all the values of the $custid array *} {section name=customer loop=$custid} id: {$custid[customer]} {/section} OUTPUT: id: 1000 id: 1001 id: 1002
|
|
Example 7-16. section loop variable 例 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]} name: {$name[customer]} address: {$address[customer]} {/section} OUTPUT: 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 |
|
Example 7-17. section names 例 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]} name: {$name[mydata]} address: {$address[mydata]} {/section} |
|
Example 7-18. nested sections 例 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]} name: {$name[customer]} address: {$address[customer]} {section name=contact loop=$contact_type[customer]} {$contact_type[customer][contact]}: {$contact_info[customer][contact]} {/section} {/section} OUTPUT: id: 1000 name: John Smith address: 253 N 45th home phone: 555-555-5555 cell phone: 555-555-5555 e-mail: john@mydomain.com id: 1001 name: Jack Jones address: 417 Mulberry ln home phone: 555-555-5555 cell phone: 555-555-5555 e-mail: jack@mydomain.com id: 1002 name: Jane Munson address: 5605 apple st home phone: 555-555-5555 cell phone: 555-555-5555 e-mail: jane@mydomain.com |
|
Example 7-19. sections and associative arrays 例 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} home: {$contacts[customer].home} cell: {$contacts[customer].cell} e-mail: {$contacts[customer].email} {/section} OUTPUT: name: John Smith home: 555-555-5555 cell: 555-555-5555 e-mail: john@mydomain.com name: Jack Jones home phone: 555-555-5555 cell phone: 555-555-5555 e-mail: jack@mydomain.com name: Jane Munson home phone: 555-555-5555 cell phone: 555-555-5555 e-mail: jane@mydomain.com |
|
Example 7-20. sectionelse 例 7-20. sectionelse 演示
{* sectionelse will execute if there are no $custid values *} {section name=customer loop=$custid} id: {$custid[customer]} {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 循环也有可供调用的变量名. 通过如下方式调用{$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},老版本的格式依然支持,但在手册的例子中只提供新的格式.