|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
filipe Smarty n00b
Joined: 12 Mar 2005 Posts: 2
|
Posted: Sat Mar 12, 2005 1:06 am Post subject: Looping functions |
|
|
Hi,
I want to make life even easier for my designers. Is there an easy way to write a function that acts as a loop? For example, if I had a Smarty function named customer_list that returned an associative array of customer information, I would like my designers to be able to:
Code: | {customer_list item="cust"}
* {$cust.id} - {$cust.name} <br>
{/customer_list}
|
rather than
Code: | {customer_list assign="cust_list"}
{foreach item="cust" from="$cust_list"}
* {$cust.id} - {$cust.name} <br>
{/foreach}
|
Is there a simple way to approach this? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
filipe Smarty n00b
Joined: 12 Mar 2005 Posts: 2
|
Posted: Sat Mar 12, 2005 1:26 am Post subject: |
|
|
I looked into that, but I wasn't sure if block functions would, by default, parse the data INSIDE the block properly. E.g., in the example I mentioned above:
Code: | {customer_list item="cust"}
* {$cust.id} - {$cust.name} <br>
{/customer_list} |
What action do I have to take to ensure {$cust.id} and {$cust.name} get written with the appropriate data? I would rather not make lists of ereg_replace functions. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Mar 12, 2005 1:52 am Post subject: |
|
|
Your block plugin has to make the assignments on every iteration so that Smarty can do the replacements.
Below is a snippet from an email exchange I had with messju not too long ago in which he presented the little code-template you see. You might be able to adapt it to your needs. Note that it is possible to do all of that work directly within the block function but breaking it into separate functions makes it look a little more generic (it was what I was seeking at the time, anyhow ).
It is untested in its current form, but asides from being a good starting point it should make clear what happens and when. The key is that your block function is getting callbacks from Smarty before and after it processes a block. It is up to your block function to: set whatever variables are needed, manipulate the processed content (if required--probably not in your case) and set the conditions for terminating the block.
[php:1:a034a72e03]
function smarty_block_boots($params, $content, &$smarty, &$repeat)
{
if (isset($content)) {
$content = smarty_block_close__boots($params, $content, &$smarty, $repeat);
} else {
smarty_block_init__boots($params, $content, $smarty, $repeat);
}
if ($repeat) {
smarty_block_open__boots($params, $content, $smarty, $repeat);
}
return $content;
}
/* called once, may set $repeat */
function smarty_block_init__boots($params, $content, &$smarty, &$repeat)
{
}
/* called on each opening of a block, may set $repeat */
function smarty_block_open__boots($params, $content, &$smarty, &$repeat)
{
}
/* called on each closing of a block, may set $repeat, returns (optionally modified) block-contents */
function smarty_block_close__boots($params, $content, &$smarty, &$repeat)
{
return $content;
}
?>[/php:1:a034a72e03]
You can use _init, for example, to setup any internal structures that you require. You will have to store your data in a static or a private member var of $smarty, a global or even directly on the tag stack. Its up to you.
You can use _open to setup any conditions that Smarty will need to process the block. For example, you can $smarty->assign(..) your vars here on each iteration.
HTH |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|