{section}, {sectionelse}
{section}
用于循环遍历“顺序索引的数据数组”,这与用于循环遍历“单个关联数组”的 {foreach}
不同。每个 {section}
标记都必须与一个结束标记 {/section}
配对使用。
注释
{foreach}
循环可以完成 {section} 循环所能做的所有操作,并且语法更简单、更方便。通常它比 {section} 循环更受欢迎。注释
{section} 循环无法循环遍历关联数组,它们必须是数字索引且按顺序排列(0、1、2、...)。对于关联数组,请使用
{foreach}
循环。
属性
特性名称 | 必需 | 描述 |
---|---|---|
name | 是 | 区段名称 |
loop | 是 | 确定循环次数 |
start | 否 | 区段开始循环的索引位置。如果值为负,则从数组的末尾开始计算开始位置。例如,如果循环数组中有七个值,且开始索引为 -2,则开始索引为 5。无效的值(位于循环数组长度之外的值)将自动截断为最接近的有效值。默认为 0。 |
step | 否 | 用于遍历循环数组的步长值。例如,step=2 将循环在索引 0、2、4 等上。如果 step 为负值,它将反向遍历数组。默认为 1。 |
max | 否 | 设置该分段将循环的最大次数。 |
show | 否 | 决定是否显示该分段(默认为 true) |
选项标志
Name | 描述 |
---|---|
nocache | 禁用对 {section} 循环的缓存 |
-
必需属性是 name 和 loop。
-
{section} 的 name 可以是任何您喜欢的名称,由字母、数字和下划线组成,就像 PHP 变量。
-
{section} 可以嵌套,并且嵌套的 {section} 名称必须彼此唯一。
-
loop 属性(通常是一个值数组)决定 {section} 将循环的次数。您还可以将整数作为循环值传递。
-
在 {section} 中打印变量时,{section} name 必须在方括号中给出变量名称旁边。
-
当循环变量中没有值时,执行 {sectionelse}。
-
{section} 也有自己的变量来处理 {section} 属性。这些属性可访问为:{$smarty.section.name.property},其中“name”是属性 name。
-
{section} 属性是
index
、index_prev
、index_next
、iteration
、first
、last
、rownum
、loop
、show
、total
。
assign()
一个数组到 Smarty
示例
输出该数组的模板
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
{section customer $custid} {* short-hand *}
id: {$custid[customer]}<br />
{/section}
<hr />
{* print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
{section foo $custid step=-1} {* short-hand *}
{$custid[foo]}<br />
{/section}
上述示例将输出
{section name=foo start=10 loop=20 step=2}
{$smarty.section.foo.index}
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
{$smarty.section.bar.index}
{/section}
上述示例将输出
{section} 的 name 可以是任何您喜欢的名称,请参阅 PHP 变量。它用于引用 {section} 中的数据。
{section name=anything loop=$myArray}
{$myArray[anything].foo}
{$name[anything]}
{$address[anything].bar}
{/section}
这是使用 {section} 打印关联数据数组的示例。以下是将 $contacts 数组分配给 Smarty 的 php 脚本。
<?php
$data = [
['name' => 'John Smith', 'home' => '555-555-5555',
'cell' => '666-555-5555', 'email' => 'john@myexample.com'],
['name' => 'Jack Jones', 'home' => '777-555-5555',
'cell' => '888-555-5555', 'email' => 'jack@myexample.com'],
['name' => 'Jane Munson', 'home' => '000-555-5555',
'cell' => '123456', 'email' => 'jane@myexample.com']
];
$smarty->assign('contacts',$data);
要输出 $contacts 的模板
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}<br />
home: {$contacts[customer].home}<br />
cell: {$contacts[customer].cell}<br />
e-mail: {$contacts[customer].email}
</p>
{/section}
上述示例将输出
<p>
name: John Smith<br />
home: 555-555-5555<br />
cell: 666-555-5555<br />
e-mail: john@myexample.com
</p>
<p>
name: Jack Jones<br />
home phone: 777-555-5555<br />
cell phone: 888-555-5555<br />
e-mail: jack@myexample.com
</p>
<p>
name: Jane Munson<br />
home phone: 000-555-5555<br />
cell phone: 123456<br />
e-mail: jane@myexample.com
</p>
此示例假定 $custid、$name 和 $address 都是包含相同数量值的数组。首先是将数组分配给 Smarty 的 php 脚本。
<?php
$id = [1001,1002,1003];
$smarty->assign('custid',$id);
$fullnames = ['John Smith','Jack Jones','Jane Munson'];
$smarty->assign('name',$fullnames);
$addr = ['253 Abbey road', '417 Mulberry ln', '5605 apple st'];
$smarty->assign('address',$addr);
loop 变量仅决定循环的次数。您可以在 {section} 中的模板中访问来自任何变量。这对于循环多个数组非常有用。您可以传递一个数组,它将通过数组大小来确定循环次数,或者传递一个整数来指定循环的次数。
{section name=customer loop=$custid}
<p>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}
</p>
{/section}
上述示例将输出
<p>
id: 1000<br />
name: John Smith<br />
address: 253 Abbey road
</p>
<p>
id: 1001<br />
name: Jack Jones<br />
address: 417 Mulberry ln
</p>
<p>
id: 1002<br />
name: Jane Munson<br />
address: 5605 apple st
</p>
{section} 可以像您希望的一样进行深度嵌套。使用嵌套 {section},您可以访问复杂的数据结构,例如多维数组。这是一个分配数组的 .php 脚本示例。
<?php
$id = [1001,1002,1003];
$smarty->assign('custid',$id);
$fullnames = ['John Smith','Jack Jones','Jane Munson'];
$smarty->assign('name',$fullnames);
$addr = ['253 N 45th', '417 Mulberry ln', '5605 apple st'];
$smarty->assign('address',$addr);
$types = [
[ 'home phone', 'cell phone', 'e-mail'],
[ 'home phone', 'web'],
[ 'cell phone']
];
$smarty->assign('contact_type', $types);
$info = [
['555-555-5555', '666-555-5555', 'john@myexample.com'],
[ '123-456-4', 'www.example.com'],
[ '0457878']
];
$smarty->assign('contact_info', $info);
此模板中$contact_type[customer]是当前客户的联系人类型数组。
{section name=customer loop=$custid}
<hr>
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}
{/section}
上述示例将输出
<hr>
id: 1000<br />
name: John Smith<br />
address: 253 N 45th<br />
home phone: 555-555-5555<br />
cell phone: 666-555-5555<br />
e-mail: john@myexample.com<br />
<hr>
id: 1001<br />
name: Jack Jones<br />
address: 417 Mulberry ln<br />
home phone: 123-456-4<br />
web: www.example.com<br />
<hr>
id: 1002<br />
name: Jane Munson<br />
address: 5605 apple st<br />
cell phone: 0457878<br />
将数据库搜索(例如 ADODB 或 PEAR)的结果分配给 Smarty
<?php
$sql = 'select id, name, home, cell, email from contacts '
."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
模板以 HTML 表格形式输出数据库结果
<table>
<tr><th> </th><th>Name</th><th>Home</th><th>Cell</th><th>Email</th></tr>
{section name=co loop=$contacts}
<tr>
<td><a href="view.php?id={$contacts[co].id}">view</a></td>
<td>{$contacts[co].name}</td>
<td>{$contacts[co].home}</td>
<td>{$contacts[co].cell}</td>
<td>{$contacts[co].email}</td>
<tr>
{sectionelse}
<tr><td colspan="5">No items found</td></tr>
{/section}
</table>
.index
如果已给定,index
包含当前数组索引,从零或start
属性开始。如果已给定,它将按一或step
属性递增。
注释
如果未修改属性
step
和start
,那么其工作方式与iteration
属性相同,不同之处在于其从零而不是一处开始。注释
$custid[customer.index]
和$custid[customer]
是相同的。
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
上述示例将输出
.index_prev
index_prev
是上一个循环索引。在第一个循环中,它将设置为 -1。
.index_next
index_next
是下一个循环索引。在最后一个循环中,如果已给定,它仍比当前索引多一个并遵守step
属性的设置。
模板以表格形式输出上述数组
{* $rows[row.index] and $rows[row] are identical in meaning *}
<table>
<tr>
<th>index</th><th>id</th>
<th>index_prev</th><th>prev_id</th>
<th>index_next</th><th>next_id</th>
</tr>
{section name=row loop=$rows}
<tr>
<td>{$smarty.section.row.index}</td><td>{$rows[row]}</td>
<td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td>
<td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td>
</tr>
{/section}
</table>
以上示例将输出一个包含下列内容的表
index id index_prev prev_id index_next next_id
0 1001 -1 1 1002
1 1002 0 1001 2 1003
2 1003 1 1002 3 1004
3 1004 2 1003 4 1005
4 1005 3 1004 5
.iteration
iteration
包含当前循环迭代并从一处开始。
注释
与
index
属性不同,它不会受{section}
属性start
、step
和max
影响。此外,iteration
从一而不是零处开始,与index
不同。rownum
是iteration
的别名,它们是相同的。
模板输出数组$arr
的每一个元素,step=2
{section name=cu loop=$arr start=5 step=2}
iteration={$smarty.section.cu.iteration}
index={$smarty.section.cu.index}
id={$custid[cu]}<br />
{/section}
上述示例将输出
iteration=1 index=5 id=3005<br />
iteration=2 index=7 id=3007<br />
iteration=3 index=9 id=3009<br />
iteration=4 index=11 id=3011<br />
iteration=5 index=13 id=3013<br />
iteration=6 index=15 id=3015<br />
另一个示例,它使用属性iteration
每五列输出一个表头块。
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.iteration is div by 5}
<tr><th> </th><th>Name</th><th>Home</th><th>Cell</th><th>Email</th></tr>
{/if}
<tr>
<td><a href="view.php?id={$contacts[co].id}">view<a></td>
<td>{$contacts[co].name}</td>
<td>{$contacts[co].home}</td>
<td>{$contacts[co].cell}</td>
<td>{$contacts[co].email}</td>
<tr>
{/section}
</table>
示例使用属性index
每三行交替一次文本颜色。
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.index is even by 3}
<span style="color: #ffffff">{$contacts[co].name}</span>
{else}
<span style="color: #dddddd">{$contacts[co].name}</span>
{/if}
{/section}
</table>
注释
“可被整除”语法是 PHP mod 运算符语法的更简单替代项。允许使用 mod 运算符:
{if $smarty.section.co.iteration % 5 == 1}
也可同样工作。注释
您还可以使用“奇数偶数交替”来反转交替。
.first
如果当前{section}
迭代是初始迭代,则将first
设置为 TRUE。
.last
如果当前部分迭代是最终迭代,则将last
设置为 TRUE。
此示例循环$customers
数组,在第一个迭代上输出一个头块,在最后输出一个尾块。还使用属性total
。
{section name=customer loop=$customers}
{if $smarty.section.customer.first}
<table>
<tr><th>id</th><th>customer</th></tr>
{/if}
<tr>
<td>{$customers[customer].id}}</td>
<td>{$customers[customer].name}</td>
</tr>
{if $smarty.section.customer.last}
<tr><td></td><td>{$smarty.section.customer.total} customers</td></tr>
</table>
{/if}
{/section}
.rownum
rownum
包含当前循环迭代,从一处开始。它是iteration
的别名,它们的工作方式相同。
.loop
loop
包含此{section}循环的最后一个索引编号。可以在{section}
内部或之后使用它。
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
There are {$smarty.section.customer.loop} customers shown above.
上述示例将输出
.show
show
用作 section 的一个参数,是一个布尔值。如果为 FALSE,则该部分将不会被显示。如果有{sectionelse}
出现,则将交替显示它。
布尔$show_customer_info
已从 PHP 应用程序传递,用于调节此部分的显示。
{section name=customer loop=$customers show=$show_customer_info}
{$smarty.section.customer.rownum} id: {$customers[customer]}<br />
{/section}
{if $smarty.section.customer.show}
the section was shown.
{else}
the section was not shown.
{/if}
上述示例将输出
.total
total
包含此 {section}
将循环的迭代数。这可以在 {section}
内部或之后使用。
{section name=customer loop=$custid step=2}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
There are {$smarty.section.customer.total} customers shown above.
另请参见 {foreach}
、{for}
、{while}
和 $smarty.section
。