smarty---section USED for loop

section,sectionelse

AttributeName             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

      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.

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 variable
{* 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. nested 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@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>


Example 7.19. sections and associative arrays
{* 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@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>


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}
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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值