今天主要讲解的是smarty的内建函数,涉及到的内建函数包括:
1、 config_load:加载配置文件
语法:<{config_load file=”配置文件名称” section=“区域名称” }>
2、Include:标签用于在当前模板中包含其它子模板.
当前模板中的变量在被包含的子模板中可用
语法:<{include file="加载文件名称"}>
3、 capture:捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面.
演示:<{capture name="one"}>
aaaaaaaaa<br>
<{/capture}>
使用smarty保留变量显示该区域内容<br>
<{$smarty.capture.one}><br>
结果:aaaaaaaaa
4、foreach,foreachelse:功能和语法与php当中类似,====foreach
5、Interation:用于显示当前循环的执行次数,从1开始,每执行一次增加 1
6、section,sectionelse:功能和效率要比foreach好
7、if,elseif,else
由于后边几个内置函数实现过程繁琐,下面通过演示一个section循环来给大家演示一下:(注:以下代码只是文件的一部分)
a.html
演示session循环的使用<br>
二维数组的显示<br>
<table border="1">
<{section loop=$data name=out}>
<tr>
<td><{$data[out].productID}></td>
<td><{$data[out].name}></td>
<td><{$data[out].price}></td>
<td><{$data[out].description}></td>
<td><{section loop=$data[out].a name=in}>
<{$data[out].a[in].one}>
<{/section}></td>
</tr>
<{sectionelse}>
数组有问题
<{/section}>
b.php
<?php
include("./init.inc.php");
$tpl->assign("name","abc");
//连接数库据,产生连接对象$mysqli
$mysqli = new mysqli("localhost","root","","product");
//执行sql语句,返回结果及对象$result
$result = $mysqli->query("select * from product");
//针对二维数组
$data=array();
while($row = $result->fetch_assoc()){
$row["a"]=array(array("one"=>"aaaaaaa"),array("two"=>"bbb"));
$data[]=$row;
}
//将$row分配到模版变量里
$tpl->assign("data",$data);
//测试if条件、分配变量
$tpl->assign("val",2);
$tpl->display("a.html");
?>
演示结果:
二维数组的显示
|
1 |
name1 |
1.02 |
good1 |
aaaaaaa |
|
2 |
name2 |
1.02 |
good2 |
aaaaaaa |
|
3 |
name3 |
1.01 |
good3 |
aaaaaaa |
|
4 |
name4 |
1.09 |
good4 |
aaaaaaa |
|
5 |
name5 |
1.08 |
good5 |
aaaaaaa |
|
6 |
name6 |
1.09 |
good6 |
aaaaaaa |
|
7 |
name7 |
1.05 |
good7 |
aaaaaaa |
|
8 |
?? |
50.54 |
???? |
aaaaaaa |
由于上述代码只是一些文件的一部分,若实现功能不完全,请多指点。
本文详细介绍了Smarty模板引擎的内建函数,包括config_load、Include、capture、foreach、section、if等,并通过示例演示了如何在实际开发中灵活运用这些函数,特别是section循环在展示二维数组数据时的高效应用。

3097

被折叠的 条评论
为什么被折叠?



