View previous topic :: View next topic |
Author |
Message |
eadz Smarty Regular
Joined: 30 Apr 2003 Posts: 61 Location: Auckland, New Zealand
|
Posted: Thu May 01, 2003 1:32 pm Post subject: smarty function plugin that acts like a section |
|
|
Hi there,
I would like to make a function that acts like a section, but the section isn't a plugin so I can't look to it for an example. How would I write a function plugin that works exactly like {section} {/section}? I.e. loops, and smarty code inbetween works as normal. |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Thu May 01, 2003 1:35 pm Post subject: |
|
|
This is not possible yet (as far as I know). But newest CVS version has support for an additional function parameter which can be used for loops. I don't know anything about it yet but perhaps you want to have a look. |
|
Back to top |
|
eadz Smarty Regular
Joined: 30 Apr 2003 Posts: 61 Location: Auckland, New Zealand
|
Posted: Thu May 01, 2003 2:04 pm Post subject: |
|
|
andre wrote: | This is not possible yet (as far as I know). But newest CVS version has support for an additional function parameter which can be used for loops. I don't know anything about it yet but perhaps you want to have a look. |
Ahh yes I see it. Addition for "repeat" param. No idea how to use it though!. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu May 01, 2003 2:27 pm Post subject: |
|
|
yes i added this recently. but i'm very indifferent if i should keep it the way it is. i think >98% of block-functions have no need for it, so i wonder if i should make it optional and not default for all block-functions, since it introduces slight overhead (really very very minimal, but a few cycles overhead nontheless).
this means: use it with caution, it's unstable/experimental!
i'm pretty sure the prototype for the repeatable block will stay: smarty_block_foo($params, $content, &$smarty, &$repeat)
but maybe blocks should not be repeatable by default, but this should be specified with register_block() (defaulting to not repeatable).
this would mean, register_block() would become:
function register_block((string)$name, (mixed)$implementeation, (bool)$cacheable, (bool)$repeatable)
if it becomes that way, then you cannot use $repeat with auto-loaded block-plugins, but only with ones you register yourself. but this will be the only change to it that may come.
i posted 2 stupid examples here:
http://marc.theaimsgroup.com/?l=smarty-dev&m=105084930710497&w=2
see $repeat as the conditional of a while loop. if your block-functions sets $repeat to true, it is called again, with new $content, else the block function is done. that's all about it. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 01, 2003 2:50 pm Post subject: |
|
|
@messju:
what if the plugin for blocks was ammended:
function smarty_block_foo($params, $content, &$smarty)
function smarty_block_start_foo($params, $content, &$smarty, &$continue)
function smarty_block_end_foo($params, $content, &$smarty, &$repeat)
If a plugin has a smarty_block_foo(), no repeats.
If a plugin has both smarty_block_start_foo() and smarty_block_start_foo(), then those two functions are called as appropriate. I changed $repeat to $continue for smarty_block_start_foo() to indicate that it aborts output if true (end is not called). The default would be to pass $continue=true and $repeat=false.
It may even be better as:
function smarty_block_start_foo($params, &$smarty, &$continue)
function smarty_block_end_foo($content, &$smarty, &$repeat)
since less data has to be passed.
2c |
|
Back to top |
|
eadz Smarty Regular
Joined: 30 Apr 2003 Posts: 61 Location: Auckland, New Zealand
|
Posted: Thu May 01, 2003 11:05 pm Post subject: |
|
|
The reason I want to make a plugin that loops, isn't so that I can specify the number of times it loops, but so that the plugin itself can decide that.
For example
Code: |
{events month="jan"}
<h1>{$event->name}</h1>
<p>{$event->desc}</p>
{/events}
|
the events plugin would go to the database, get the events of the month jan, and loop through the results. I'm not sure how the varibles would work, but in this case $event would be reassigned at every loop.
I think functionality like this would be very cool! Being able to make a plugin that loops through results must surely be very useful!
[edit] also I think it's very imporntant that this can work without having to register the plugin! [/edit] |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 01, 2003 11:15 pm Post subject: |
|
|
@eadz:
Do you mean:
Code: |
$_events =& $events->get_month("jan");
$smarty->assign_by_ref('events', $_events);
-----
{section name="event" loop=$events}
<h1>{$events[event].name}</h1>
<p>{$events[event].desc}</p>
{/section}
|
?? |
|
Back to top |
|
eadz Smarty Regular
Joined: 30 Apr 2003 Posts: 61 Location: Auckland, New Zealand
|
Posted: Fri May 02, 2003 7:09 am Post subject: |
|
|
boots wrote: | @eadz:
Do you mean:
Code: |
$_events =& $events->get_month("jan");
$smarty->assign_by_ref('events', $_events);
-----
{section name="event" loop=$events}
<h1>{$events[event].name}</h1>
<p>{$events[event].desc}</p>
{/section}
|
?? |
Yes, like that but all as a function/block plugin.
The idea is to use smarty to write an easily extendable app with smarty. Not much core code, and to use smarty's plugin functionality, to easily create plugins for the application. so with
Code: |
php:
$Smarty->assign("events",$myEvents->get_Events("jan"));
template:
{section name="event" loop=$events}
<h1>{$events[event].name}</h1>
<p>{$events[event].desc}</p>
{/section}
|
.... which requires changing the core code, to :
Code: |
template:
{events month="jan"}
<h1>{$events[event].name}</h1>
<p>{$events[event].desc}</p>
{/events}
|
Having to register the plugin via the api would defeat the purpose of just having a plugin.
I think this functionality is obviously very useful, and I'm desprate to find a way to enable this. I'm not sure if it's the block code or the section code I should be looking at. |
|
Back to top |
|
eadz Smarty Regular
Joined: 30 Apr 2003 Posts: 61 Location: Auckland, New Zealand
|
Posted: Fri May 02, 2003 11:20 am Post subject: |
|
|
messju wrote: | yes i added this recently. but i'm very indifferent if i should keep it the way it is. i think >98% of block-functions have no need for it, so i wonder if i should make it optional and not default for all block-functions, since it introduces slight overhead (really very very minimal, but a few cycles overhead nontheless).
|
I think a function plugin that loops through an array is 98% more useful than a block function |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri May 02, 2003 6:04 pm Post subject: |
|
|
@eadz:
it looks like you aren't looking for new functionality at all, but instead a different syntax.
You can easily do what you want by wrapping the appropriate code in a template and then {including} that template with parameters.
You don't like {include}? Well, you can create a plugin that internally does a $smarty->fetch($tpl).
Even though I think looping can be improved, I don't think that that is what you want.
IMHO |
|
Back to top |
|
eadz Smarty Regular
Joined: 30 Apr 2003 Posts: 61 Location: Auckland, New Zealand
|
Posted: Sat May 03, 2003 4:48 am Post subject: |
|
|
boots wrote: | @eadz:
it looks like you aren't looking for new functionality at all, but instead a different syntax.
<...>
Even though I think looping can be improved, I don't think that that is what you want.
IMHO |
I think the difference between a plugin and a non-plugin is more then syntax. There are many ways to loop through an array from the database, but none that can be done cleanly in a plugin. The additions to the block plugin code may help.
I want to get it as easy as possable for designers, so
Code: |
{events month="jan"}
{entries.date|date_format:"%d"}
<h2>{entries.title}</h2>
{/entries}
|
is the cleanest syntax.
I have tried making a plugin like so, named block.events.php:
Code: |
function smarty_block_events($params, $content, &$smarty) {
global $db;
if (!isset($content)) { /* first call */
if (!isset($params['var'])) $varname = "events";
$newcontent = $smarty->left_delimiter.'section name=n loop=$'.$varname.$smarty->right_delimiter;
$results = $db->get_results("select * from events");
$smarty->assign($varname,$results);
return $newcontent;
} else {
return $content.$smarty->left_delimiter.'/section'.$smarty->right_delimiter;
}
}
|
But it doesn't work ( it seems that smarty evaluates smarty returned code in a weird way ) |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat May 03, 2003 7:01 am Post subject: |
|
|
Your plugin code seems to assume that the content is still un-executed--this is false. You can't write "dynamic" plugin code. What you can do is put the code that you want to be executed into a template and do a $smarty->fetch(tpl) from your plugin. |
|
Back to top |
|
|