Block Functions
| From: | Jeremy Sisson | Date: | Thu, 14 Nov 2002 17:17:43 +0000 |
| Subject: | Block Functions | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-6436@lists.php.net to get a copy of this message | ||
I have been playing around with foreach and section blocks, trying to find a way to do a simple loop. I need something just like a for loop that can count from x to y, etc. Foreach doesn't seem to be able to do it, and section can't seem to do it without a 'loop' variable to look at.
So, I tried to play around with block functions;
(The function)
function smarty_block_for($params, $content, &$smarty) {
if ($content) {
for ($j=$params['start']; $j<=$params['stop']; $j++) {
echo $content;
}
}
}
(The template)
{for start=1 stop=3}
Pants<br />
{/for}
This does indeed output;
Pants
Pants
Pants
My next objective, however, is to setup some sort of 'index' variable, that I can use in the template to show the current iteration. My immediate thought was something like this;
(The function)
function smarty_block_for($params, $content, &$smarty) {
if ($content) {
for ($j=$params['start']; $j<=$params['stop']; $j++) {
$smarty->assign('index', $j);
echo $content;
}
}
}
(The template)
{for start=1 stop=3}
Pants {$index}<br />
{/for}
But this didn't work. I also tried just assigning '$index = $j;' instead of the smarty assign line.
What I need is some way to assign a variable that can be used in the block function contents...Does anyone have any suggestions? Perhaps a way to make foreach or section work like this?
J