Heim >Backend-Entwicklung >PHP-Tutorial >Smarty 3.1.8 中的注册函数 registerPlugin使用问题

Smarty 3.1.8 中的注册函数 registerPlugin使用问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:55:481377Durchsuche

在学习smarty中遇到了这样一个问题
     在php文件中我这样写  

function demo($params){        extract($params);        echo $a.$b;    }    $tpl->registerPlugin('block','demo','demo');



然后在模板 文件中这样写:

<body>        <{demo a=1 b=text}>        <{/demo}>    </body>





输出的时候怎么输出了两个  1text  1text


求解决!


回复讨论(解决方案)

去掉这个“ ”?

去掉的话 就会发生错误

木写过插件,这这例子有帮助没?
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}

问题还是没有解决


估计block的插件会调用两次

例子我看了  例子还是出现了错误

例子我看了  例子还是出现了错误


block方式的会执行两次。通过 if (isset($content)) {,做了判断所以只执行了一次。
http://www.itlearner.com/code/smarty_cn/plugins.block.functions.html

引用 11 楼 to_phper 的回复:

例子我看了  例子还是出现了错误

block方式的会执行两次。通过 if (isset($content)) {,做了判断所以只执行了一次。
http://www.itlearner.com/code/smarty_cn/plugins.block.functions.html


3q 问题解决了!!

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}>


输出:
Array ( [size] => 30 [color] => #ff0000 ) Array ( [size] => 30 [color] => #ff0000 ) 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn