Smarty中section的用法

一、循環一個簡單的一維數組:php

<?phpweb

    $data = array(1000,1001,1002);sql

    $smarty->assign('custid',$data);數據庫

?>數組

//customer和下面的foo能夠隨便命名,做用其實僅僅是一個index下標,用來引用數組中的元素app

{section name=customer loop=$custid}    oop

  id: {$custid[customer]}<br />post

{/section}spa

<hr />blog

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

二、不用assign數組直接在smarty中循環:

//特別地設置了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

三、section的name的值是隨你定的

Example 7-32. Naming a {section}

{section name=anything loop=$myArray}

  {$myArray[anything].foo}

  {$name[anything]}     //這種用法目前還沒怎麼用過,也不太清楚

  {$address[anything].bar}    //這種也是

{/section}

四、遍歷一個關聯數組,嵌套的數組

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

五、從數據庫查詢記錄顯示,其實是顯示二維數組,其實同上例同樣

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

六、嵌套的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 />

相關文章
相關標籤/搜索