register_..." />
smarty自定义函数用法示例
smarty自定义函数用法示例
发布时间:2016-12-29 来源:查字典编辑
摘要:本文实例讲述了smarty自定义函数用法。分享给大家供大家参考,具体如下:register_function("liuyibao","tes...

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

<?php require_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.="<psize']}em;color:{$args['color']}'>{$args['con']}</p>"; } return $str; } //自定义一个块方式函数 //调用方法<{test1}><{/test1}> function test2($args,$con){ $str=""; for($i=0;$i<$args['times'];$i++){ $str.="<psize']}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"); ?>

PS:这里推荐几款本站的格式化/美化/转换工具可以帮助你整理杂乱无章的代码,相信大家在以后的开发中能够用得上:

php代码在线格式化美化工具:

http://tools.jb51.net/code/phpformat

JavaScript代码美化/压缩/格式化/加密工具:

http://tools.jb51.net/code/jscompress

在线XML格式化/压缩工具:

http://tools.jb51.net/code/xmlformat

JSON代码格式化美化工具:

http://tools.jb51.net/code/json

在线XML/JSON互相转换工具:

http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:

http://tools.jb51.net/code/jsoncodeformat

sql代码在线格式化美化工具:

http://tools.jb51.net/code/sqlcodeformat

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新php教程学习
热门php教程学习
编程开发子分类