smarty模板引擎中自定义函数的方法

(编辑:jimmy 日期: 2024/9/29 浏览:2)

本文实例讲述了smarty 自定义函数方法,分享给大家供大家参考。具体如下:

本实例目的:输出 times 次 con的内容(输出4次hello world)

文件1:
复制代码 代码如下:<"./libs/Smarty.class.php");
$smarty = new Smarty();
//自定义一个函数
//说明:(1)、$arr为一个数组;(2)、tpl调用形式{test times="4" size="5" con="hello,world" color="red"}
function test($arr){
 $str = "";
 for($i=0;$i<$arr['times'];$i++){
  $str .= "<font size='".$arr['size']."' color='".$arr['color']."'>".$arr['con']."</font>";
 }
 return $str;
}
//注册函数 registerPlugin
$smarty->registerPlugin("function","test","test");//第二个参数是模板文件调用的函数名称,可变;第三个参数是上面自定义的函数名称;相应于一个对应关系

$smarty->display("temp.tpl");
"codetitle">复制代码 代码如下:<html>
<h2>smarty自定义函数的使用</h2>
{test times="3" con="hello world" size="3" color="green"}
</html>
注意:smarty 3.1.8 已经不支持注册函数 register_function,应换成 registerPlugin

希望本文所述对大家的php程序设计有所帮助。

一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。