PHP模板引擎Smarty内建函数foreach,foreachelse用法分析

时间:2022-09-24 21:24:31

本文实例讲述了PHP模板引擎Smarty内建函数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 数据)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--
-- 表的结构 `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 (模板初始化文件)

?
1
2
3
4
5
6
7
8
9
<?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(主文件)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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 的模板文件)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<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>

执行结果:

PHP模板引擎Smarty内建函数foreach,foreachelse用法分析

最后总结下,主文件 index.php 中传递过去的数组 $_html 为二维数组。保留变量 $smarty.foreach 的使用都是基于 {foreach} 标记中的 name 属性,使用到的保留变量属性有:first(首条记录)、last(末条记录)、iteration(总是从 1 开始,每执行一次增加 1)、total(用于显示循环执行的次数)

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。