|
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 |
Synchro Smarty Regular
Joined: 27 Apr 2008 Posts: 43
|
Posted: Tue Sep 17, 2013 9:03 am Post subject: Making a looping block plugin |
|
|
I'm looking to build some kind of block tag that loops over its contents, a bit like a foreach with parameters. One use for this is in rendering an RSS feed, I'd like a tag like this:
{rss url="http://..." items=5}
<h1>{$rss_item_title}</h1>
<p>{$rss_item_body}</p>
{/rss}
I'd have the outer block tag loop over its own contents and assign the values to the variables used inside it.
I have no problem making a block plugin, but I'm not sure how I could make it loop like that. I may just need a smack round the head of course. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Sep 17, 2013 8:03 pm Post subject: |
|
|
Some thing like this
Code: |
function smarty_block_rss($params, $content, $template, &$repeat) {
if (is_null($content)) {
// openening tag call
static $items = $params['item'];
}
// assign your {$rss...} vars
.....
$items--;
if ($item) $repeat = true;
return $content;
} |
|
|
Back to top |
|
Synchro Smarty Regular
Joined: 27 Apr 2008 Posts: 43
|
Posted: Mon Oct 28, 2013 5:17 pm Post subject: |
|
|
So near yet so far…
I have the repeat calling mechanism working nicely, stepping through my RSS feed, but the problem is that $content is already rendered by the time the block plugin receives it, so assigning vars does nothing. The docs do actually say this:
Quote: | Note that the template block will have already been processed by Smarty, so all you will receive is the template output, not the template source. |
This appears to be true: what I receive in $content has all smarty markup removed. The net result is that while this structure in the block plugin works, it achieves nothing because all smarty syntax is gone by the time it receives the content, so the changesd variables will never be displayed.
I'm not sure how this can be since there are block plugins that can be wrapped around other tags.
Is there something I missed? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Oct 28, 2013 6:47 pm Post subject: |
|
|
The plugin is called once with $content = null at the opening tag
Code: | {rss url="http://..." items=5} |
after that it repeats rendering everything between opening and closing tag. The result is in $content. You will never see Smarty tags in $content.
Inside the plugin you must assign your variables with the assign() methode
Example
Code: | function smarty_block_rss($params, $content, $template, &$repeat) {
if (is_null($content)) {
// opening tag call
static $items = $params['item'];
}
// assign your {$rss...} vars
$template->assign('rss_item_body',$items);
$template->assign('rss_item_title',"tittle $items");
.....
$items--;
if ($item) $repeat = true;
return $content;
} |
|
|
Back to top |
|
Synchro Smarty Regular
Joined: 27 Apr 2008 Posts: 43
|
Posted: Mon Oct 28, 2013 8:12 pm Post subject: |
|
|
That's mostly what I was doing, but I found my problem. I was doing this:
Code: | $template->smarty->assign('rss_item_body',$items); |
instead of
Code: | $template->assign('rss_item_body',$items); |
Which is why it was coming out blank. I'd assumed that I needed to use the smarty instance, as function plugins use, rather than the template instance. Thanks for the fix. |
|
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
|