{section}, {sectionelse}
{section} 用于循环遍历顺序索引的数组数据,与 {foreach} 不同,后者用于遍历单个关联数组。每个 {section} 标签必须配对一个结束的 {/section} 标签。
注意
{foreach}循环可以实现所有 {section} 循环的功能,并且其语法更简单易懂。因此,通常我们更倾向于使用 {foreach} 循环,而不是 {section} 循环。注意
{section} 循环不能遍历关联数组,它们必须是数值索引且顺序排列(0,1,2,...)。对于关联数组,应使用
{foreach}循环。
属性
| 属性名称 | 是否必须 | 描述 |
|---|---|---|
| name | 是 | section 的名称 |
| loop | 是 | 确定循环次数的值 |
| start | 否 | section 开始循环的索引位置。如果值为负数,起始位置从数组的末尾计算。例如,如果循环数组中有七个值,start 为-2,那么起始索引为 5。无效的值(超出循环数组长度的值)会自动截断到最接近的有效值。默认为 0。 |
| step | 否 | 用于遍历循环数组的步长值。例如,step=2 将在索引 0、2、4 等上进行循环。如果 step 为负数,将反向遍历数组。默认为 1。 |
| max | 否 | 设置 section 循环的最大次数。 |
| show | 否 | 确定是否显示此 section(默认为 true) |
选项标志
| 名称 | 描述 |
|---|---|
| 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
示例
<?php
$data = [1000, 1001, 1002];
$smarty->assign('custid', $data);
输出数组的模板
{* 此示例将打印出 $custid 数组的所有值 *}
{section name=customer loop=$custid}
{section customer $custid} {* 简写 *}
id: {$custid[customer]}<br />
{/section}
<hr />
{* 逆序打印出 $custid 数组的所有值 *}
{section name=foo loop=$custid step=-1}
{section foo $custid step=-1} {* 简写 *}
{$custid[foo]}<br />
{/section}
上述示例将输出:
id: 1000<br />
id: 1001<br />
id: 1002<br />
<hr />
id: 1002<br />
id: 1001<br />
id: 1000<br />
{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}
上述示例将输出:
10 12 14 16 18
<hr />
20 18 16 14 12 10
{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">未找到任何项目</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}
上述示例将输出:
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
.index_prev
index_prev 是上一个循环的索引。在第一次循环时,它的值为-1。
.index_next
index_next 是下一个循环的索引。在最后一次循环时,它仍然比当前索引多一,如果给定了 step 属性,那么它将尊重该属性的设置。
<?php
$data = [1001,1002,1003,1004,1005];
$smarty->assign('rows',$data);
将上述数组输出到表格的模板
{* $rows[row.index] 和 $rows[row] 在含义上是相同的 *}
<table>
<tr>
<th>索引</th><th>id</th>
<th>前一个索引</th><th>前一个id</th>
<th>下一个索引</th><th>下一个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>
上述示例将输出包含以下内容的表格:
索引 id 前一个索引 前一个id 下一个索引 下一个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 包含当前循环迭代,从一开始。
注意
这不受
{section}属性start、step和max的影响,与index属性不同。iteration也是从一开始,而不是从零开始。rownum是iteration的别名,它们 功能相同。
<?php
// 数组从 3000 到 3015
$id = range(3000,3015);
$smarty->assign('arr', $id);
模板输出 $arr 数组的每个其他元素,因为 step=2
{section name=cu loop=$arr start=5 step=2}
迭代={$smarty.section.cu.iteration}
索引={$smarty.section.cu.index}
id={$custid[cu]}<br />
{/section}
上述示例将输出:
迭代=1 索引=5 id=3005<br />
迭代=2 索引=7 id=3007<br />
迭代=3 索引=9 id=3009<br />
迭代=4 索引=11 id=3011<br />
迭代=5 索引=13 id=3013<br />
迭代=6 索引=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>
一个示例,使用 iteration 属性每三行改变一次文本颜色。
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.iteration is even by 3}
<span style="color: #ffffff">{$contacts[co].name}</span>
{else}
<span style="color: #dddddd">{$contacts[co].name}</span>
{/if}
{/section}
</table>
注意
"is div by" 语法是 PHP mod 运算符语法的一个更简单的替代。允许使用 mod 运算符:
{if $smarty.section.co.iteration % 5 == 1}将达到相同的效果。注意
你也可以使用 "is odd by" 来反转交替。
.first
如果当前 {section} 迭代是初始的,那么 first 被设置为 TRUE。
.last
如果当前 section 迭代是最后的,那么 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}
上面显示了 {$smarty.section.customer.loop} 个客户。
上述示例将输出:
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
上面显示了 3 个客户。
.show
show 被用作 section 的参数,是一个布尔值。如果
FALSE,section 将不会被显示。如果存在 {sectionelse}
那么将交替显示。
布尔值 $show_customer_info 已经从 PHP 应用程序中传递,
来调节是否显示这个 section。
{section name=customer loop=$customers show=$show_customer_info}
{$smarty.section.customer.rownum} id: {$customers[customer]}<br />
{/section}
{if $smarty.section.customer.show}
section 被显示了。
{else}
section 没有被显示。
{/if}
上述示例将输出:
1 id: 1000<br />
2 id: 1001<br />
3 id: 1002<br />
section 被显示了。
.total
total 包含这个 {section} 将要
循环的迭代次数。这可以在 {section} 内部或之后使用。
{section name=customer loop=$custid step=2}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
上面显示了 {$smarty.section.customer.total} 个客户。
也可以参见 {foreach},
{for}, {while}
和 $smarty.section.