首页 > 语言 > PHP > 正文

smarty自定义函数用法示例

2024-05-04 23:46:31
字体:
来源:转载
供稿:网友
这篇文章主要介绍了smarty自定义函数用法,结合实例形式详细分析了Smarty自定义函数的定义与使用技巧,需要的朋友可以参考下
 

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

<?phprequire_once "smarty.config.php";//自定义一个函数//调用方法:<{test1 times="4" size="5" con="Hello,Liuyibao!" color="red"}>function test1($args){$str="";for($i=0;$i<$args['times'];$i++){$str.="<p style='font-size:{$args['size']}em;color:{$args['color']}'>{$args['con']}</p>";}return $str;}//自定义一个块方式函数//调用方法<{test1}><{/test1}>function test2($args,$con){$str="";for($i=0;$i<$args['times'];$i++){$str.="<p style='font-size:{$args['size']}em;color:{$args['color']}'>{$con}</p>";}return $str;}//定义一个计算方法function jisuan($args){switch($args['operate']){case "+" :$res=$args['num1']-$args['num2'];break;case "-" :$res=$args['num1']-$args['$num2'];break;case "*" :$res=$args['num1']*$args['$num2'];break;case "/" :$res=$args['num1']/$args['$num2'];break;}return $res;}//注册一下$smarty->register_function("liuyibao","test1");//注册块函数$smarty->register_block("liuyibao2","test2");//注册函数jisuan$smarty->register_function("jisuan","jisuan");//替换变量$smarty->display("function.tpl");?>
 


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选