smarty中自定义函数的使用(包括块方式)
来源:互联网 发布:编程器哪个牌子的好 编辑:程序博客网 时间:2024/05/15 06:46
myfun.tpl
<span style="font-size:18px;"><title>自定义函数</title><h1>自定义函数的使用配置</h1><{*这是注释*}><meta http-equiv="content-type" content="text/html;charset=utf-8"/><hr><{myfun times="10" con="Hello world, this is user defined function!" color="red" size="5"}><br>now time is <{date_now format="%Y/%m/%d"}><{*下面是自定义块函数调用*}><br>下面是块标签<br><{blockTest times="10" color="green" size="5"}>Hello world,this is block function test!<{/blockTest}></span>
userdefinefunction.php
<span style="font-size:18px;"><?phprequire_once './libs/Smarty.class.php';date_default_timezone_set ( "Asia/Chongqing" );header ( '<meta http-equiv="content-type" content="text/html;charset=utf-8/>"' );$smarty = new Smarty ();$smarty->left_delimiter = "<{";$smarty->right_delimiter = "}>";// 注册plugin函数中的三个参数// type defines the type of the plugin. Valid values are "function", "block", "compiler" and "modifier".// name defines the name of the plugin.// callback defines the PHP callback. it can be either:$smarty->registerPlugin ( "function", "myfun", "myfun" );function myfun($args) {$str = "";for($i = 0; $i < $args ['times']; $i ++) {$str .= "<font size='" . $args ['size'] . "' color='" . $args ['color'] . "'>" . $args ['con'] . "</font><br>";}return $str;}$smarty->registerPlugin ( "function", "date_now", "print_current_date" );function print_current_date($params, $smarty) {if (empty ( $params ["format"] )) {$format = "%b %e, %Y";} else {$format = $params ["format"];}return strftime ( $format, time () );}$smarty->registerPlugin ( "block", "blockTest", "blockTest" );// 自定义函数(块方式)function blockTest($args, $con) {$str = "";// 此处注意为什么要加empty判断,如果不加此判断的话,在$con为空的情况下,$str也会被输出十次,从而造成页面中有很多的空行,这种问题的原因是// 因为smarty计算在前,你获取数据在后,也就是说第一次时,$con已经被smarty计算过了(虽然$con没有值),但是确实在smarty内部计算过了,而且之后也进行了输出if (! empty ( $con )) {for($i = 0; $i < $args ['times']; $i ++) {$str .= "<font size='" . $args ['size'] . "' color='" . $args ['color'] . "'>" . $con . "</font><br>";}} // echo $str;return $str;}$smarty->display ( "myfun.tpl" );?></span>
0 0
- smarty中自定义函数的使用(包括块方式)
- 【smarty】smarty中自定义函数和自定义块
- smarty中自定义函数
- php学习笔记(三十六)smarty中使用自定函数和代码块(smarty2和smarty3的不同)
- smarty的自定义函数
- smarty的自定义函数
- 在smarty中自定义函数
- 如何在smarty模板中使用自定义函数
- smarty内建函数和自定义插件函数的使用
- 【smarty】smarty自定义函数
- smarty 块函数文件的注译
- smarty的block function(块函数)
- 在块中调用自定义的函数
- 07_Android操作sqllite数据库(包括2中方式操作数据的方式),单元测试,BaseAdapter的使用,自定义view的综合使用案例
- smarty自定义函数的应用
- PHP学习之路之Smarty自定义函数(中)
- Smarty中自定义函数与调用方法
- php学习笔记(三十五)smarty模板中使用变量的三种方式
- switch的处理方法
- hdu 4831 Scenic Popularity(模拟)
- 最短路算法汇总
- 今天股市大跌的原因
- C#进度显示的简单实现
- smarty中自定义函数的使用(包括块方式)
- vs在release下调试时局部变量值错位修复
- android
- 批量下载音效
- CSS解决无空格太长的字母,数字不会自动换行的问题
- java反射理解
- 利用Eclipse中的Maven构建Web项目(二)
- SOA、ESB、NServiceBus、云计算 总结
- spring下载和安装