在学习smarty中遇到了这样一个问题
在php文件中我这样写
function demo($params){ extract($params); echo $a.$b; } $tpl->registerPlugin('block','demo','demo');
<body> <{demo a=1 b=text}> <{/demo}> </body>
去掉这个“ <{/demo}>”?
去掉的话 就会发生错误
木写过插件,这这例子有帮助没?
http://www.xuandun.net/173/
这个我看了
我就是按照这个来写的
呵呵,我试试
smarty 3.1.7例子没有问题
$smarty = new Smarty;$smarty->registerPlugin("function","demo", "demotest");;//制定类型,这里制定是函数调用,指定模板函数名称和php函数名称function demotest($params) { extract($params);//获取参数a与b了 echo $a.$b;//输出}
{demo a="2" b="test"}
$smarty->registerPlugin("function","demo", "demotest")
$smarty->registerPlugin("block","demo", "demotest")
问题还是没有解决
你看看手册中的示例
// function declarationfunction do_translation ($params, $content, $smarty, &$repeat, $template){ if (isset($content)) { $lang = $params["lang"]; // do some translation with $content return $translation; }}// register with smarty$smarty->registerPlugin("block","translate", "do_translation");
{translate lang="br"}Hello, world!{/translate}
问题还是没有解决
例子我看了 例子还是出现了错误
例子我看了 例子还是出现了错误
引用 11 楼 to_phper 的回复:
例子我看了 例子还是出现了错误
block方式的会执行两次。通过 if (isset($content)) {,做了判断所以只执行了一次。
http://www.itlearner.com/code/smarty_cn/plugins.block.functions.html
block会出现两次,算是smarty的一个bug吧。但不影响使用。
function test2($arr,$content){ print_r($arr); //$html = "<div style='font-size:".$arr["size"]."px;color:".$arr["color"]."'>".$content."</div>"; //return $html;}$smarty->registerPlugin("block","test2","test2");
<{test2 size="30" color="#ff0000"}>Hello World<{/test2}>