smarty中section循环

smaty遇到了这种循环,用foreach就不行了,只有section了

<div class="mContentList">

<span><a href="#" target="_blank">巡检司令肌肤</a></span>

<span><a href="#" target="_blank">斯蒂芬懒得说</a></span>

</div>

<div class="mContentList">

<span><a href="#" target="_blank">巡检司令肌肤</a></span>

<span><a href="#" target="_blank">斯蒂芬懒得说</a></span>

</div>

<div class="mContentList">

<span><a href="#" target="_blank">巡检司令肌肤</a></span>

<span><a href="#" target="_blank">斯蒂芬懒得说</a></span>

</div>

<div class="mContentList">

<span><a href="#" target="_blank">巡检司令肌肤</a></span>

<span><a href="#" target="_blank">斯蒂芬懒得说</a></span>

</div>

 

我这样用的:

 

{section loop=$chugui name=bar step=2}
          <div class="mContentList">

               <span><a href="http://abc.com/fc0newscon_{$chugui[bar].Id}.html" target="_blank">{$chugui[bar].Second_Title}</a></span>

              <span><a href="http://abc.com/fc0newscon_{$chugui[$smarty.section.bar.index+1].Id}.html" target="_blank">{$chugui[$smarty.section.bar.index+1].Second_Title}</a></span> </div>
  {/section}

还比较方便,顺便记录一下section用法吧

 

1、循环一个简单的一维数组:
Example 7-30. Looping a simple array with {section}

<?php
    $data = array(1000,1001,1002);
    $smarty->assign('custid',$data);
?>

//customer和下面的foo可以随便命名,作用其实仅仅是一个index下标,用来引用数组中的元素
{section name=customer loop=$custid}    
  id: {$custid[customer]}<br />
{/section}
<hr />

{section name=foo loop=$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 />

2、不用assign数组直接在smarty中循环:
Example 7-31. {section} without an assigned array

//特别地设置了start,step属性用来控制循环
//$smarty.section.section的名字.index是一个特殊变量,用来显示当前循环的位置
{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

4、遍历一个关联数组,嵌套的数组

<?php
$data = array(
          array('name' => 'John Smith', 'home' => '555-555-5555',
                'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
          array('name' => 'Jack Jones', 'home' => '777-555-5555',
                'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
          array('name' => 'Jane Munson', 'home' => '000-555-5555',
                'cell' => '123456', 'email' => 'jane@myexample.com')
        );
$smarty->assign('contacts',$data);
?>

//section不用嵌套,因为只有一个数组,数组内部用$contacts[customer]得到
//每个数组,再用.键名来得到键值
{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}

The above example will output:

<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>
5、从数据库查询记录显示,实际上是显示二维数组,其实同上例一样
<?php
$sql = 'select id, name, home, cell, email from contacts '
      ."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
?>

//结果:

<table>
<tr><th>&nbsp;</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>

6、嵌套的section
<?php

$id = array(1001,1002,1003);
$smarty->assign('custid',$id);

$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);

$addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);

$types = array(
           array( 'home phone', 'cell phone', 'e-mail'),
           array( 'home phone', 'web'),
           array( 'cell phone')
         );
$smarty->assign('contact_type', $types);

$info = array(
           array('555-555-5555', '666-555-5555', 'john@myexample.com'),
           array( '123-456-4', 'www.example.com'),
           array( '0457878')
        );
$smarty->assign('contact_info', $info);

?>

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

The above example will output:

<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 />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值