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

前端技术 2023/09/06 PHP

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

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

文件1:

复制代码 代码如下:
<?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\");
?>

本文地址:https://www.stayed.cn/item/15160

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。