1
mboyden
Smarty {while} Loops
  • 2010/6/17 15:26

  • mboyden

  • Moderator

  • Posts: 484

  • Since: 2005/3/9 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:
<?php /** * Smarty plugin * @package Smarty * @subpackage plugins */ /** * Smarty {while} compiler function plugin * * Type: compiler function<br> * Name: while<br> * Date: Sep 08 2006<br> * Purpose: provide a while-loop<br> * @author Frank Habermann <lordlamer at lordlamer dot de> * @param string containing var-attribute and value-attribute * @param Smarty_Compiler * Examples: The following code will loop 5 times ;) * <pre> * {assign var="test" value=1} * {while ($test <= 5)} * {assign var="test" value="`$test+1`"} * jo * {/while} * </pre> */ $this->register_compiler_function('/while', 'smarty_compiler_endwhile'); function smarty_compiler_while($tag_arg, &$smarty) { $res = $smarty->_compile_if_tag($tag_arg); preg_match("/<?php if (.*): ?>/",$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.
Pessimists see difficulty in opportunity; Optimists see opportunity in difficulty. --W Churchill

XOOPS: Latest | Debug | Hosting and Web Development

Login

Donat-O-Meter

Stats
Goal: $15.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $15.00
Make donations with PayPal!

Latest GitHub Commits