源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

smarty自定义函数用法示例

  • 时间:2022-10-11 21:48 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:smarty自定义函数用法示例
本文实例讲述了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.="<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");
?>

[b]PS:这里推荐几款本站的格式化/美化/转换工具可以帮助你整理杂乱无章的代码,相信大家在以后的开发中能够用得上:[/b] [b]php代码在线格式化美化工具: [/b][url=http://tools.jb51.net/code/phpformat]http://tools.jb51.net/code/phpformat[/url] [b]JavaScript代码美化/压缩/格式化/加密工具: [/b][url=http://tools.jb51.net/code/jscompress]http://tools.jb51.net/code/jscompress[/url] [b]在线XML格式化/压缩工具: [/b][url=http://tools.jb51.net/code/xmlformat]http://tools.jb51.net/code/xmlformat[/url] [b]JSON代码格式化美化工具: [/b][url=http://tools.jb51.net/code/json]http://tools.jb51.net/code/json[/url] [b]在线XML/JSON互相转换工具: [/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url] [b]json代码在线格式化/美化/压缩/编辑/转换工具: [/b][url=http://tools.jb51.net/code/jsoncodeformat]http://tools.jb51.net/code/jsoncodeformat[/url] [b]sql代码在线格式化美化工具: [/b][url=http://tools.jb51.net/code/sqlcodeformat]http://tools.jb51.net/code/sqlcodeformat[/url] 更多关于Smarty相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/26.htm]smarty模板入门基础教程[/url]》、《[url=http://www.1sucai.cn/Special/350.htm]PHP模板技术总结[/url]》、《[url=http://www.1sucai.cn/Special/192.htm]PHP基于pdo操作数据库技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部