smarty模板引擎中自定义函数的方法
本文实例讲述了smarty自定义函数方法,分享给大家供大家参考。具体如下:
本实例目的:输出times次con的内容(输出4次helloworld)
文件1:
<?php
//创建smarty对象
require_once("./libs/Smarty.class.php");
$smarty=newSmarty();
//自定义一个函数
//说明:(1)、$arr为一个数组;(2)、tpl调用形式{testtimes="4"size="5"con="hello,world"color="red"}
functiontest($arr){
$str="";
for($i=0;$i<$arr['times'];$i++){
$str.="<fontsize='".$arr['size']."'color='".$arr['color']."'>".$arr['con']."</font>";
}
return$str;
}
//注册函数registerPlugin
$smarty->registerPlugin("function","test","test");//第二个参数是模板文件调用的函数名称,可变;第三个参数是上面自定义的函数名称;相应于一个对应关系
$smarty->display("temp.tpl"); ?>
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特