smarty registerPlugin 无法正常工作

smarty registerPlugin doesn't work correctly

我有一个非常简单的代码,我简化了我的代码以帮助您理解确切的问题

<?php
require_once('theme/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->setTemplateDir('theme/site/');
$smarty->setCompileDir('theme/compile');
$smarty->setConfigDir('theme/config');
$smarty->setCacheDir('theme/cache');
$smarty -> plugins_dir = 'theme/libs/plugins/';
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}';

function reg_combobox($params, $content, &$smarty, &$repeat){
$str="";
$str.="<select>";
$str.="<option  value=\"0\"  >please select </option>";
for($i=0;$i<2;$i++)
$str.="<option>$i</option>";
$str.="</select>";
return $str;
}
$smarty->registerPlugin('block','mycombobox', 'reg_combobox');

echo $smarty->fetch('index.tpl');
?>

和index.tpl:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>
    {mycombobox}{/mycombobox}
</body>
</html>

一切看起来都很好,但我的浏览器支持 2 个下拉列表:


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>
<select><option  value="0"  >please select </option><option>0</option><option>1</option>
</select>
<select>
<option  value="0"  >please select </option><option>0</option><option>1</option>
</select>
</body>
</html> 

为什么? 我的代码有什么问题?

一个月后,终于找到解决办法了!

参数$repeat 通过引用传递给函数实现,并为其提供了控制块显示次数的可能性。默认情况下,$repeat 在第一次调用 block-function(开始标记)时为 TRUE,在所有后续调用块函数(块的结束标记)时为 FALSE。每次 $repeat 为 TRUE 的函数实现 returns 时,计算 {func}...{/func} 之间的内容,并使用参数 $content 中的新块内容再次调用函数实现。

在return之前我添加了:

if($repeat)