使用smarty模版自定义函数,程序如下:
TestController3.php
<?php
require_once "./libs/Smarty.class.php";
//创建一个smarty
$smarty=new Smarty;
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";//自定义一个函数
//tpl调用形式<{hsp times="4" size="5" con="hello,顺平" color="red"}>
function test1($args){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font color='".$args['color']."' size='".$args['size']."'>".$args['con']."</font>";
}
return $str;
}//自定义一个函数(块方式)
//<{fun1 times="100" color="red" size="5"}>hello<{/fun1}>
function test2($args,$con){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font color='".$args['color']."' size='".$args['size']."'>".$con."</font>";
}
return $str;
} //注册一下
$smarty->register_function("hsp","test1");
//注册块函数
$smarty->register_block("fun1","test2"); $smarty->display("test3.tpl");
?>
test3.tpl程序:
<h1>自定义函数的使用</h1>
<{hsp times="100" con="hello,北京" color="red" size="5"}>
<h1>块调用</h1>
<{fun1 times="100" color="blue" size="5"}>hello<{/fun1}>
程序无法输出内容,总是报错:
Fatal error: Uncaught exception 'SmartyException' with message 'Call of unknown method 'register_function'.' in C:\xampp\htdocs\mycode\smarty1\libs\sysplugins\smarty_internal_templatebase.php:806 Stack trace: #0 C:\xampp\htdocs\mycode\smarty1\TestController31.php(16): Smarty_Internal_TemplateBase->__call('register_functi...', Array) #1 C:\xampp\htdocs\mycode\smarty1\TestController31.php(16): Smarty->register_function('test1', 'test1') #2 {main} thrown in C:\xampp\htdocs\mycode\smarty1\libs\sysplugins\smarty_internal_templatebase.php on line 806
请高手指教,十分感谢
TestController3.php
<?php
require_once "./libs/Smarty.class.php";
//创建一个smarty
$smarty=new Smarty;
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";//自定义一个函数
//tpl调用形式<{hsp times="4" size="5" con="hello,顺平" color="red"}>
function test1($args){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font color='".$args['color']."' size='".$args['size']."'>".$args['con']."</font>";
}
return $str;
}//自定义一个函数(块方式)
//<{fun1 times="100" color="red" size="5"}>hello<{/fun1}>
function test2($args,$con){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font color='".$args['color']."' size='".$args['size']."'>".$con."</font>";
}
return $str;
} //注册一下
$smarty->register_function("hsp","test1");
//注册块函数
$smarty->register_block("fun1","test2"); $smarty->display("test3.tpl");
?>
test3.tpl程序:
<h1>自定义函数的使用</h1>
<{hsp times="100" con="hello,北京" color="red" size="5"}>
<h1>块调用</h1>
<{fun1 times="100" color="blue" size="5"}>hello<{/fun1}>
程序无法输出内容,总是报错:
Fatal error: Uncaught exception 'SmartyException' with message 'Call of unknown method 'register_function'.' in C:\xampp\htdocs\mycode\smarty1\libs\sysplugins\smarty_internal_templatebase.php:806 Stack trace: #0 C:\xampp\htdocs\mycode\smarty1\TestController31.php(16): Smarty_Internal_TemplateBase->__call('register_functi...', Array) #1 C:\xampp\htdocs\mycode\smarty1\TestController31.php(16): Smarty->register_function('test1', 'test1') #2 {main} thrown in C:\xampp\htdocs\mycode\smarty1\libs\sysplugins\smarty_internal_templatebase.php on line 806
请高手指教,十分感谢
解决方案 »
- 关于mysql字段求和并排名次的问题
- PHP页面不显示mysql中内容
- 数字分开保存
- 怎么样实现窗口关闭后还运行程序.在线等..
- 用fsockopen去打开https://localhost/get.php,正确读取的流程是怎样的?我试了不成功,读不到数据.代码见内.
- 上传图片时,我用javascript取出图片的长宽,怎么延时,才可以取出正确的
- PHP如何在Windows2000下进行配置
- php中如何把数据库里的数据导成EXECL文件...............救命........(
- 请问一下thinkphp 查询条件 单引号转义的问题
- 为什么两个页面的读到的COOKIE不一致
- 数据库更新某整数字段最小的一条数据, 怎么写?
- 今个自己写的代码,打印菱形
string name,
mixed callback,
bool cacheable,
mixed cache_attrs);
该函数将以插件的形式来注册函数或者方法。 参数如下: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:A string containing the function nameAn array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string containing the method-nameAn array of the form array($class, $method) with $class being the class name and $method being a method of the class.大多数情况下cacheable 和 cache_attrs可被省略。 参见缓存能力设置它们的值。
所以在你注册函数时应该这样注册:
//注册一下
$smarty->registerPlugin("function","hsp","test1");
//注册块函数
$smarty->registerPlugin("block","fun1","test2");