Re: [SMARTY-DEV] repeatable blocks

From: Date: Sun, 20 Apr 2003 14:34:30 +0000
Subject: Re: [SMARTY-DEV] repeatable blocks
References: 1  Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-1231@lists.php.net to get a copy of this message
maybe i should post some (useless) examples to illustrate this a bit: [block.repeat_simple.php] <?php function smarty_block_repeat_simple($params, $content, &$smarty, &$repeat) { static $count; if (!isset($content)) { /* first call */ if (!isset($params['count'])) $smarty->trigger_error('count needed'); $count = $params['count']; } else { $count--; } $repeat = $count>0; /* indicate loop */ return $content; } ?> [test.tpl] {repeat_simple count=12} huhu {/repeat_simple} dumps "huhu\n" 12 times. the difference to just doing " for ($i=0; $i<$count; $i++) { echo $content; } " inside the plugin becomes obvious if you use things like {count} and {cycle} in-between {block_simple}...{/block_simple} the example above doesn't handle nested repeats, so here is a better one: [block.repeat.php] <?php function smarty_block_repeat($params, $content, &$smarty, &$repeat) { static $counts = null; if (!is_array($counts)) $counts=array(); /* done just once! */ if (!isset($params['count'])) $smarty->trigger_error('count needed'); if (!isset($content)) /* push count at first call */ array_push($counts, (int)$params['count']); $repeat = $counts[count($counts)-1]-- >0; /* indicate loop */ if (!$repeat) array_pop($counts); return $content; } ?> [test.tpl]: {repeat count=3} row {repeat count=2} col {/repeat} /row {/repeat} this would make the output look: row col col /row row col col /row row col col /row both examples are not good. the above can be accomplished more easily with {section} for example than writing a plugin. but there maybe problems you cannot solve with {section} and {foreach} that easily. as a rule of thumb: always try to put the logic in the php if you see your templates get too complicated !!! you often get things done most easily if you provide the data you want to display in a structure that smarty likes and it is far easier/cleaner/better to do this in php than in the template. so see this as a last resort and not as the proposed way to loop through your data. {section} and {foreach} will do it best in far more than 95% of the cases i guess. this one is just for the little rest :) greetings messju On Sun, Apr 20, 2003 at 01:32:58PM +0000, messju mohr wrote: > hello smarty-dev, > > i added a new (4th) parameter to block-functions recently. the > parameter is boolean, called $repeat and passed by reference to the > function. > > so the function prototype to reads now > > function smarty_block_foo($params, $content, &$smarty, &$repeat) > > you shouldn't notice any difference in the behaviour of your > block-functions regardless wether you use the old or the new > prototype. if you see a difference, please report this as a bug to > me. :) > > > $repeat is set to true on the first call to your block-function. > $repeat is set to false on any subsequent call to your block-function > > this is nothing new, one could also say: > $repeat = !isset($content) > but since the parameter is passed by reference it can be maninpulated > by your block-function-plugins. > > by setting $repeat inside your block-function to true, you can tell > smarty to "repeat" the block. that is: call the block-function another > time with the content between {foo}...{/foo} evaluated another time > and assigned to the $content-parameter. as a special case you can also > set $repeat to false on the very first-call to you block-functions, to > make the block between {foo}...{/foo} not evaluated at all. that's all > behind it. > > > i hope you like it. report if you encounter problems, but give also > feedback, if you have useful applications for it :) > > > greetings > messju > > -- > Smarty Development Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.smarty.dev (#1231) next »