phpsmartysmarty3

smarty registerPlugin doesn't work correctly


I have a very simple code, I have simplified my code to help you understand the exact problem

<?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');
?>

and index.tpl:

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

everything looks fine but my browser shoes 2 drop-down lists:


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

why? what is wrong with my codes?


Solution

  • After one month, finally, I found the solution!

    The parameter $repeat is passed by reference to the function implementation and provides a possibility for it to control how many times the block is displayed. By default $repeat is TRUE at the first call of the block-function (the opening tag) and FALSE on all subsequent calls to the block function (the block's closing tag). Each time the function implementation returns with $repeat being TRUE, the contents between {func}...{/func} are evaluated and the function implementation is called again with the new block contents in the parameter $content.

    before return I added:

    if($repeat)