PHP模板引擎Smarty內建函數foreach,foreachelse用法及執行個體分析

來源:互聯網
上載者:User
這篇文章主要介紹了PHP模板引擎Smarty內建函數foreach,foreachelse用法,結合執行個體形式分析了foreach,foreachelse的功能與具體提示,需要的朋友可以參考下

在 Smarty 模板中,您可以使用 foreach 來重複一個區塊。而在模板中則需要從 PHP 中分配過來一個數組。這個數組可以是多維陣列。Smarty 中 {foreach} 標記和 PHP 中 foreach 相同,不同的是它們的一個在模板檔案中使用,一個在 PHP 指令碼中使用。因此,文法會不同。但是,它們的作用都是相同的,即遍曆數組中的內容。與 {foreach} 標記相對的還有一個 {foreachelse} 標記,{foreachelse} 標記的作用是:如果數組為空白,那麼就執行該標記內的內容。 模板中 {foreach} 和 {/foreach} 必須是成對的出現,它有四個參數,其中, from 和 item 兩個參數是必要的。關於它的參數請看下面列表:


屬性 類型 是否必須 預設值 描述
from string Yes n/a 待迴圈數組的名稱
item string Yes n/a 當前處理元素的變數名稱
key string No n/a

當前處理元素的鍵名

name string No n/a 該迴圈的名稱,用於訪問該迴圈


我們通過一個執行個體,來示範 Smarty 中 {foreach} 和 {foreachelse} 的使用。

執行個體思路:從資料庫中取出內容,賦給一個陣列變數 $_html ,再給這個陣列變數分配給模板,然後在模板中進行該數組的遍曆

test.sql (使用到的 SQL 資料)

---- 表的結構 `user`--CREATE TABLE IF NOT EXISTS `user` ( `id` mediumint(8) unsigned NOT NULL auto_increment, `username` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `addTime` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;---- 轉存表中的資料 `user`--INSERT INTO `user` (`id`, `username`, `email`, `addTime`) VALUES(1, '蒼井空', 'canjingkong@sina.com.cn', '2011-10-24 00:00:00'),(2, '櫻木花道', 'ymhd@163.com', '2011-10-24 00:00:00'),(3, '赤木晴子', 'chimiqingzi@yahoo.com,cn', '2011-10-24 00:00:00'),(4, '流川楓', 'lcfeng@sina.com', '0000-00-00 00:00:00'),(5, '蠟筆小新', 'labixiaoxin@sina.com', '2011-10-24 00:00:00'),(6, '金剛葫蘆娃', 'jghlw@sina.com', '2011-10-24 00:00:00');

init.inc.php (模板初始設定檔案)

<?php define('ROOT_PATH', dirname(__FILE__)); //設定網站根目錄 require ROOT_PATH.'/libs/Smarty.class.php'; //載入 Smarty 模板引擎 $_tpl = new Smarty(); //建立一個執行個體對象 $_tpl->template_dir = ROOT_PATH.'/tpl/'; //重新指定模板目錄 $_tpl->compile_dir = ROOT_PATH.'./com/'; //重新指定編譯目錄 $_tpl->left_delimiter = '<{'; //重新指定左定界符 $_tpl->right_delimiter = '}>'; //重新指定右定界符?>

index.php(主檔案)

<?php require 'init.inc.php'; //引入模板初始設定檔案 global $_tpl; $_mysqli = new mysqli(); //建立一個 mysqli() 對象 $_mysqli->connect('localhost','root','資料庫密碼','資料庫名'); //串連資料庫,請您自行設定 $_mysqli->set_charset('utf8'); //設定編碼 $_result = $_mysqli->query("select username,email,addTime from user order by id asc"); $_html = array(); while (!!$_row=$_result->fetch_assoc()) {  $_html[] = $_row; } $_tpl->assign('data',$_html); //把數組分配到模板中 $_tpl->display('index.tpl'); //引入模板 $_mysqli->close(); //關閉資料庫,釋放資源?>

tpl/index.tpl(主檔案 index.php 的模板檔案)

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>foreach,foreachelse</title></head><body> <table align="center" border="1" width="800">  <{foreach from=$data item="row" name="ls"}> <!-- 這個foreach 迴圈分配過來的數組有幾行資料 -->   <!-- 在此,我們做幾個保留變數 $smarty.foreach 的操作 -->   <!-- 當資料顯示第一條的時候,第一行的表格背景為黃色,使用屬性:first -->   <!-- 當資料顯示最後一條的時候,最後一行的表格背景為藍色,使用屬性:last -->   <!-- 顯示下分配過來的數組的總個數,使用屬性:total -->   <{if $smarty.foreach.ls.first}>   <tr bgcolor="#FFFF00"> <!-- 第一行背景為黃色 -->   <{elseif $smarty.foreach.ls.last}>   <tr bgcolor="#0000FF"> <!-- 最後一行背景為藍色 -->   <{else}>   <tr>   <{/if}>    <td><{$smarty.foreach.ls.iteration}></td><!-- 注意:這裡是保留變數 $smarty.foreach 的使用,iteration:總是從 1 開始,每執行一次增加 1 -->    <{foreach from=$row item="col" name="lsin"}> <!-- 這個foreach 迴圈數組內的內容,顯示在表格的<td></td>標籤裡 -->     <td><{$col}></td>    <{/foreach}>   </tr>  <{foreachelse}> <!-- 如果分配過來的數組中沒有資料,那麼就執行下面的操作! -->   <tr>    <td>對不起!暫時沒有資料。</td>   </tr>  <{/foreach}>  <tr>   <td colspan="4" align="center">分配數組的總記錄數為:<{$smarty.foreach.ls.total}>條</td>  </tr> </table></body></html>

執行結果:

最後總結下,主檔案 index.php 中傳遞過去的數組 $_html 為二維數組。保留變數 $smarty.foreach 的使用都是基於 {foreach} 標記中的 name 屬性,使用到的保留變數屬性有:first(首條記錄)、last(末條記錄)、iteration(總是從 1 開始,每執行一次增加 1)、total(用於顯示迴圈執行的次數)

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.