使用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
请高手指教,十分感谢
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");