將一個數組輸出到模板中來處理2

1. MSmarty_class.php

<?php
/*@file:class/MSmarty_class.php
 * 
 * @title:類,將使用smarty的前三個步聚封起
 * 
 * @author:virhuiai
 *
 * @date:03-21-2013
 * 
 * @modified:
 * 03-23-2013
 * 
 * 
*/


date_default_timezone_set('PRC');
//require_once('/usr/lib64/php/Smarty/Smarty.class.php');
require_once( 'Smarty/Smarty.class.php' );

class MSmarty extends Smarty{
	function __construct(){
		parent::__construct();

		$this->setTemplateDir('/home/virhuiai/html/test/smarty/templates');
		$this->setCompileDir('/home/virhuiai/html/test/smarty/templates_c');
		$this->setConfigDir('/home/virhuiai/html/test/smarty/configs');
		$this->setCacheDir('/home/virhuiai/html/test/smarty/cache');

		$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
		$this->assign('app_name', 'Guest Book');
	}
}
$smarty = new MSmarty();
//$smarty->right_delimiter='{{';
//$smarty->right_delimiter = '}}';

$right_delimiter='{{';
$right_delimiter = '}}';

//$smarty->assign('name','virhuiai');

//$smarty->display('setup.tpl');

?>

2.try_seciton.php


<?php
/*@file:try_seciton.php
 *
 * @title:類,將使用smarty的前三個步聚封起
 *
 * @author:virhuiai
 *
 * @date:03-21-2013
 *
 * @modified:03-23-2013
 *
 */

include_once './class/MSmarty_class.php';

$arr = array(
array('id'=>1,'title'=>'title1'),
array('id'=>2,'title'=>'title2'),
array('id'=>3,'title'=>'title3')
);

$smarty->assign('news',$arr);

$smarty->display('try_section.tpl');
?>
3.try_seciton.tpl

{section name=sn loop=$news}
   {if $smarty.section.sn.first}
       <table>
       <th>id</th>
       <th>title</th>
   {/if}
   <tr>
       <td>{$news[sn].id}</td>
       <td>{$news[sn].title}</td>
    </tr>
    {if $smarty.section.sn.last}
        </table>
    {/if}
{sectionelse}
    there is no news.
{/section}

顯示結果以下(是一個表格的樣子,如下顯示的沒有加樣式): php

 id title 
 1 title1 
 2 title2 
 3 title3 

能夠看出,不管是索引仍是關聯數組用起來都是很方便。下面介紹下section中各個屬性的說明: html

一、section中的屬性 數組

name:(必選) 是section循環的名稱只是標示循環惟一的名字沒有特別意義,前面沒有$符號; app

loop: (必選)是在php聲明中的變量名稱,用來標示是循環哪個數組(即要循環數組名)須要使用$; oop

start: (可選)循環執行的初始位置. 若是該值爲負數,開始位置從數組的尾部算起. 例如:若是數組中有7個元素,指定start爲-2,那麼指向當前數組的索引爲5. 非法值(超過了循環數組的下限)將被自動調整爲最接近的合法值. ui

step: (可選)如其它語言的循環,是一個步長,若是爲負數,則倒序循環; this

max:(可選)循環的最大下標,若是是1則只循環1次,若是爲2則循環2次; spa

show:(可選)默認爲true即顯示。若是設置了{sectionelse}。表示若是數組沒有內容的時候顯示這部分的內容;若是show爲false則顯示這部分。若是沒有設置{sectionelse}則不輸出該數組。 code

 

二、smarty中section中的變量 htm

index:用於顯示當前循環的索引,從0開始(若是指定了start屬性,那麼由該值開始),每次加1(若是指定了step屬性,那麼由該值決定).若是沒有指定step和start屬性,此值的做用和iteration相似,只不過從0開始而已.

index_prev:用於顯示上一個循環索引值. 循環開始時,此值爲-1.

index_next:用於顯示下一個循環索引值. 循環執行到最後一次時,此值仍然比當前索引值大1(若是指定了step,取決於此值).

iteration:用於顯示循環的次數.iteration 不像index屬性受start、step和max屬性的影響,該值老是從1開始(index是從0開始的).rownum 是iteration的別名,二者等同.

first:若是當前循環第一次執行,first 被設置爲true.

last:若是當前循環執行到最後一次,last 被設置爲true.

rownum:用於顯示循環的次數. 該屬性是iteration的別名,二者等同.

loop:用於顯示該循環上一次循環時的索引值. 該值能夠用於循環內部或循環結束後.

show:是 section 的參數. show 取值爲布爾值 true 或 false. 若是設置爲 false,該循環將不顯示. 若是指定了 sectionelse 子句,該字句是否顯示也取決於該值.

total:用於顯示循環執行總的次數. 能夠在循環中或執行結束後調用此屬性.

相關文章
相關標籤/搜索