想了解smarty模板引擎中自定义函数的方法的相关内容吗,在本文为您仔细讲解smarty模板引擎中自定义函数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:smarty,模板,引擎,自定义,函数,方法,下面大家一起来学习吧。
<?php
//创建smarty对象
require_once("./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");
?>