1
I'd been lamenting that there is no {while} loop in Smarty. I understand some of the reasoning why they haven't done it, but I've found times where I really needed it and had to hit the functional layer which I'd rather avoid when possible. So, I came across a solution that works if you want/need it.
Create a file in XOOPS_ROOT/class/smarty/xoops_plugins called compiler.while.php and put this code into it:
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {while} compiler function plugin
*
* Type: compiler function
* Name: while
* Date: Sep 08 2006
* Purpose: provide a while-loop
* @author Frank Habermann
* @param string containing var-attribute and value-attribute
* @param Smarty_Compiler
* Examples: The following code will loop 5 times ;)
*
* {assign var="test" value=1}
* {while ($test <= 5)}
* {assign var="test" value="`$test+1`"}
* jo
* {/while}
*
*/
$this->register_compiler_function('/while', 'smarty_compiler_endwhile');
function smarty_compiler_while($tag_arg, &$smarty) {
$res = $smarty->_compile_if_tag($tag_arg);
preg_match("//",$res,$token);
return "while " . $token[1] . " {";
}
function smarty_compiler_endwhile($tag_arg, &$smarty) {
return "}";
}
?>
Save it and you're done. Then u can use it just like the example shown. Works for me; hope it works for you.
I found this
Smarty while loop solution in the Smarty forums.