内建函数
按照smarty的要求去用
config_load :加载配置文件
file= “配置文件名称”
section=“区域名称”
$tpl->configs=”./configs” //基准路径
./configs/dir_1/a.conf
<{config_load file=” dir_1/a.conf” section=“one” }>
例如:
configs/include.conf:
border=1
bgcolor=red
emphstart=<b>
emphend=</b>
ErrorCity=You must supply a <{#emphstart#}>city<{#emphend#}>;
ErrorState=You must supply a <{#emphstart#}>state<{#emphend#}>;
a. html:
<{#smarty.config.border#}>
<{#smarty.config.bgcolor#}>
<{config_load file=”include.conf”}>
<{eval var=#ErrorCity#}>
<{eval var=#ErrorState#}>
include :
标签用于在当前模板中包含其它子模板.
当前模板中的变量在被包含的子模板中可用
<{include file=”header.tpl”}>
<{include file=”footer.html”}>
例如:
header.html:
这是头文件
<html>
<head>
<title>演示<{$name}>用法</title>
</head>
</html>
b.php:
$tpl->assign(“name”,”include”);
a.html:
<{include file=”header.html”}>
下面是分配过来的<{$name}>变量
捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面.
任何在 {capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定.
在模板中通过 $smarty.capture.foo 访问该变量.
如果没有指定 name 属性,函数默认将使用 "default" 作为参数.
{capture}必须成对出现,即以{/capture}作为结尾,该函数不能嵌套使用.
<{capture name=”自定义区域名”}>
aAaaaaaaaaaaaaa
<{$name}>
<{include file=”head.tpl”}>
……
<{/capture}>
<{$smarty.capture.自定义区域名}>
例如:
a. html:
<{capture name=”gao”}>
Today is Thursday
<{$name}>
<{/capture}>
调用:
<{$smarty.capture.gao}><br>
<{$smarty.capture.gao}>
功能和语法与php当中类似,====foreach
foreach($row as $key=>$value){
…
}
效率差;
for($i=0;$i<5;$i++){ }
例如:
b.php:
//从数据库获取数据:
$mysqli=new mysqli(“localhost”,”root”,”123”,”smarty_fenye”);
$result=$mysqli->query(“select * from tbl_product”);
//针对索引数组
$row=$result->fetch_row();
$tpl->assign(“data”,$row);
a.html:
<{foreach from=$data item=value}>
<{$value}><br>
<{foreachelse}>
数据有问题
<{/foreach}>
针对二维数组:
b.php:
$data=array();
while($row=$result->fetch_assoc){
$data[]=$row;
}
$tpl->assign(“data”,$data);
a.html:
<table border=1>
<{foreach from=$data item=value name=out}>
<{if $smarty.foreach.out.first}>
<tr bgcolor=red>
<{elseif $smarty.foreach.out.end}>
<tr bgcolor=green>
<{/if}>
<{foreach from=$value item=col name=in}>
<td><{$col}></td>
<{foreachelse}>
数据有问题
<{/foreach}>
</tr>
<{foreachelse}>
数据有问题
<{/foreach}>
外层循环共有<{$smarty.foreach.out.total}>次
里层循环共有<{$smarty.foreach.in.total}>次
</table>
变量
$smarty.foreach.循环名.变量名
iteration :用于显示当前循环的执行次数,从1开始,每执行一次增加 1
first :
section,sectionelse (推荐)for
功能和效率要比foreach好
只针对索引数组
section 显示二维数组 loop
section 不能处理下标不连续的数组
例如:
b.php:
$data=array();
while($row=$result->fetch_assoc){
$data[]=$row;
}
$tpl->assign(“data”,$data);
a. html:
<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>
</tr>
<{sectionelse}>
该数组有问题
<{/section}>
</table>
eq(相等)、ne(不等)、neq(不等)、gt
(大于)、lt(小于)、lte(小于等于)、le(小于等于)、gte(大于等于)、ge(大于等于)、>、<、<=、>=.==、!=、
is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、
eq等同于==
例如:
b.php:
$tpl->assign(“val”,2);
a. html:
is even:如果是偶数
<{if $val is even}>
aaaaaaa
<{else}>
bbbbb
<{/if}>
结果:aaaaaaa