陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数
描述: smarty模板技术为PHP程序设计中处理和界面的分离提供了很大的便利,有必要了解内建函数等方面的内容,方便贵阳网站建设人员加深对smarty模板处理机制的了解。陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数
一、什么是内建函数
内建函数是无需定义可以直接使用的函数。smarty模板引擎提供了一系列的内建函数(bulid-in functions) 。smarty模板文件是以标签的方式来调用函数。
这里我们先给大家介绍对数组进行操作的 foreach ,foreachelse 函数的使用,其余都会陆续介绍。
(1)搭建环境
在网站\htdocs\smarty目录中,创建文件testcontroller2.php,在templates目录中创建test2.tpl。smarty放在./libs/。
(2)取普通索引数组
在testcontroller2.php中加代码:
require_once "./libs/smarty.class.php";
$smarty=new smarty();
$smarty->left_delimiter="<{";//一般都要写在Display方法之前。
$smarty->right_delimiter="}>";
$arr1=array('上海','天津','北京');
$smarty->assign("arr1",$arr1);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<h1>内建函数</h1>
<br/>********取内建普通索引数组列表*********<br/>
<{foreach from=$arr1 item=temp}>
<{$temp}>
<{/foreach}>
<br/>
输出:
********取内建索引数组列表*********
上海天津北京
也可以带key来取值,<{foreach from=$arr1 item=temp key=kk}>
<{$kk}>=<{$temp}>
输出:
0=上海1=天津2=北京
(3)取普通关联一维数组
可以直接取出。
在testcontroller2.php中加代码:
$arr2=array('city1'=>'上海','city2'=>'天津','city3'=>'北京');
$smarty->assign("arr2",$arr2);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取普通关联一维数组列表*********<br/>
<{foreach from=$arr2 item=temp}>
<{$temp}>
<{/foreach}>
<br/>
(4)取普通关联一维数组(带键值)
在testcontroller2.php中加代码:
$arr2=array('city1'=>'上海','city2'=>'天津','city3'=>'北京');
$smarty->assign("arr2",$arr2);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取普通关联一维数组列表(带键值)*********<br/>
<{foreach from=$arr2 item=temp key=k}>
<{$k}>=<{$temp}>
<{/foreach}>
<br/>
输出:
********取普通关联一维数组列表(带键值)*********
city1=上海 city2=天津 city3=北京
(5)取二维索引数组
在testcontroller2.php中加代码:
//索引的二组数据
$arr3=array(array('上海','天津','北京'),array('小债','老妖','财神'));
$smarty->assign("arr3",$arr3);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取二维索引数组列表*********<br/>
<{foreach from=$arr3 item=temp key=k}>
<{$temp[0]}>||<{$temp[1]}>||<{$temp[2]}>||<br/>
<{/foreach}>
<br/>
输出:
********取二维索引数组列表*********
上海||天津||北京||
小债||老妖||财神||
当内容数组少一个或者多一个的时候,就取出出错。即某个数组没有值时产生错误,取不出来,也不报错。这种方式不太灵活。
(6)取二维索引数组(带子循环)
可以对temp进行再次循环。
在testcontroller2.php中加代码:
$arr3=array(array('上海','天津','北京'),array('小债','老妖','财神','黄世仁'));
$smarty->assign("arr3",$arr3);
在test2.tpl文件中加代码:
<br/>********取二维索引数组列表(带子循环)*********<br/>
<{foreach from=$arr3 item=temp key=k}>
<{foreach from=$temp item=val}>
<{$val}>
<{/foreach}>
<{/foreach}>
<br/>
输出:
********取二维索引数组列表(带子循环)*********
上海天津北京小债老妖财神黄世仁
(7)取二维关联数组
在testcontroller2.php中加代码:
//关联的二组数据
$arr4=array(array('id'=>'a001','email'=>'xiaoin@sqq.com','age'=>23),array('id'=>'a002','email'=>'12333@sqq.com','age'=>66));
$smarty->assign("arr4",$arr4);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取二维关联数组列表*********<br/>
<{foreach from=$arr4 item=temp1 key=k}>
<{foreach from=$temp1 item=temp2 key=kk}>
<{$kk}>=<{$temp2}>
<{/foreach}>
<br/>
<{/foreach}>
<br/>
输出:
********取二维关联数组列表*********
id=a001 email=xiaoin@sqq.com age=23
id=a002 email=12333@sqq.com age=66
(8)foreach if—elseif—else
显示多个人物信息,可以从数据库取出,这里简单演示即可。
$emplist=array(array('id'=>'100','name'=>'小明',"age"=>40),array('id'=>'110','name'=>'大明',"age"=>80));
要求使用smarty模板,显示所有学员信息,但是不显示年龄。
在调用函数的时候,如果在模板tpl的双引号中使用到php文件的各种数据,参看双引号里值的嵌入章节(第53讲:PHP smarty模板配置及变量操作, http://www.by-sj.com/WebRes/Pro789.html)。
在testcontroller2.php中加代码:
$arr4=array(array('id'=>'a001','email'=>'xiaoin@sqq.com','age'=>12),array('id'=>'a002','email'=>'12333@sqq.com','age'=>66));
$smarty->assign("arr4",$arr4);
在test2.tpl文件中加代码:
<br/>********foreach if—elseif—else*********<br/>
<h1>取出二维关联数组{age<5 提示你是小孩 age>=5 age<=18 年轻人 age>18 成年人}</h1>
<{foreach from=$arr4 item=item1}>
<{*二次循环*}>
<{if $item1.age < 5}>
<{$item1.id}> 你是小孩
<{elseif $item1.age >=5 and $item1.age<=18 }>
<{$item1.id}>年轻人
<{elseif $item1.age>18}>
<{$item1.id}>成年人
<{/if}>
<br/>
<{/foreach}>
输出:
********foreach if—elseif—else*********
取出二维关联数组{age<5 提示你是小孩 age>=5 age<=18 年轻人 age>18 成年人}
a001年轻人
a002成年人
(9)内建函数的综合性案例
**************循环取出emplist不显示age <br/>
<{foreach from=$emplist item=emp}>
<{foreach from=$emp item=val key=k}>
<{if $k != "age"}>
<{$k}>=<{$val}>
<{/if}>
<{/foreach}>
<br/>
<{/foreach}>
* 显示 多个人物信息.
***show.php***页面
<?php
include_once './libs/smarty.php';
$test2[]=array("name"=>"张平","date"=>"1999-11-11");
$test2[]=array("name"=>"张平2","date"=>"2000-11-11");
$smarty->assign("title2",$test2);
$test3=array("北京","天津","上海");
$smarty->assign("test3",$test3);
//如果给出的数组有键值对,我们怎么处理,比如我们表示一个人的信息
$test4=array("name"=>"红本","电话"=>"120","email"=>"zhangsan@sohu.com");
$smarty->assign("test4",$test4);
$smarty->display("index.tpl");
//var_dump($test2);
?>
****对应的index.tpl文件*****
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<hr/>
********循环取出变量**************<br/>
{section name=abc loop=$title2}
{$title2[abc].name}-{$title2[abc].date}<br/>
{/section}
<hr/>
{* 这个是注释哈哈 *}
********从数组单个单个的取出值************<br/>
{$test3[0]} | {$test3[1]} | {$test3[2]}<br/>
*********使用foreach取出数组的值*********<br/>
{foreach from=$test3 item=city_name}
城市名: {$city_name}<br>
{/foreach}
*********使用foreach取出数组的key-val对<br/>
{foreach from=$test4 item=item key=key}
{if $key neq "email"}
个人信息 : {$key} : {$item}<br/>
{else}
这个是email 信息,不能给你看!
{/if}
{/foreach}<br/>
***********变量操作符**************<br/>
{"now"|date_format:"%Y/%m/%d"}
{$smarty.now|date_format:"%Y/%m/%d %H:%M:%S"}
</body>
</html>
***********怎么理解smarty这种函数调用机制.*********
****show.php****
<?php
require_once 'smarty_inc.php';
//这是注册一个函数.
$smarty->register_function("myfun1","test");
//自定义函数
function test($args){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size=7 color='blue'>".$args['content']."</font>";
}
return $str;
}
$smarty->display("templates/index.tpl");
?>
***index.tpl****
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
{myfun1 times="5" content="hello,张三"}
</html>
***smarty还有一种块方式调用标签的方式*******
**show.php****
<?php
require_once 'smarty_inc.php';
$smarty->register_function("myfun1","test");
//自定义函数
function test($args){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size=7 color='blue'>".$args['content']."</font>";
}
return $str;
}
$tt="水浒传好看";
$smarty->assign("con1",$tt);
//smarty 块的方式调用函数.
$smarty->register_block("myfun2","test2");
//test2($args,$content,&$t,&$j) 块函数后面两个参数不用传递
function test2($args,$content){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size='".$args['size']
."' color='".$args['color']."'>".$content."</font>";
}
return $str;
}
$smarty->display("templates/index.tpl");
?>
****对应的index.tpl模板文件***
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
{myfun1 times="5" content="hello,张三"}
{myfun2 times="2" color="green" size="7"}
北京{$con1}
{/myfun2}
</html>
【推荐阅读】
陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数