View previous topic :: View next topic |
Author |
Message |
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Sun Sep 12, 2004 1:09 pm Post subject: $this pointer instead of e.g. $smarty.foreach.name.varname |
|
|
Hi all,
it would be nice, if there were a possibility to use somethink like an "$this" pointer inside of foreach/section loops
Code: |
{section name=section_loop loop=$Links step=3}
{ $this.index }
{ $this.iteration }
{ $this.last }
{/section}
|
instead of:
Code: |
{section name=section_loop loop=$Links step=3}
{ $smarty.section.tr.index }
{ $smarty.section.tr.iteration }
{ $smarty.section.tr.last }
{/section}
|
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Sep 12, 2004 6:15 pm Post subject: |
|
|
Yeah... but this should work okay if you really need to do a lot of those lookups:
Code: | {section name=tr loop=$Links step=3}
{assign var=this value=$smarty.section.tr }
{ $this.index }
{ $this.iteration }
{ $this.last }
{/section}
|
|
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Mon Sep 13, 2004 6:39 am Post subject: |
|
|
boots wrote: | Yeah... but this should work okay if you really need to do a lot of those lookups:
Code: | {section name=tr loop=$Links step=3}
{assign var=this value=$smarty.section.tr }
{ $this.index }
{ $this.iteration }
{ $this.last }
{/section}
|
|
you are right
i thought about an smarty side solution, because of i think the actual way is realy involved. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Sep 13, 2004 3:14 pm Post subject: |
|
|
$smarty is the only reserved variable name in the template namespace. This would introduce $this as a new reserved var, possibly breaking BC.
What would be even more intuitive (but surely cause problems with BC):
Code: | {section name=tr loop=$Links step=3}
{ $tr.index }
{ $tr.iteration }
{ $tr.last }
{/section} |
That way if you have nested section loops, the references will still be explicit enough. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 13, 2004 5:38 pm Post subject: |
|
|
@monte: that's the same one I proposed the other day, so I'm in agreement. The impact of the BC issue is hard to gauge so it seems a little unlikely. Perhaps if such a feature was only activated when a new attribute (instead of name) was used (say ID)?
Code: | {section id=tr loop=$Links step=3}
{ $tr.index }
{ $tr.iteration }
{ $tr.last }
{/section}
|
This way someone would have to explicitly use this technique so BC issues shouldn't occur. It may be more obvious to use an extra attribute though I wouldn't know what to call it. Either the attribute would be a flag to make the assignment based on the name attribute or it would give the name of the var to use within the section loop.
On another note, according to the docs, $SCRIPT_NAME is also reserved. http://smarty.php.net/manual/en/language.variables.smarty.php |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Sep 13, 2004 8:16 pm Post subject: |
|
|
Yes, $SCRIPT_NAME was kept for BC, it was originally a default global_assign variable. |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Wed Sep 15, 2004 6:14 am Post subject: |
|
|
what does BC means?
what about using a new attribute called "this"
Code: |
{section this="tr" loop=$Links step=3}
{ $tr.index }
{ $tr.iteration }
{ $tr.last }
{/section}
|
same for foreach loops |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Sep 15, 2004 6:56 am Post subject: |
|
|
kills wrote: | what does BC means? |
BC = backwards compatible |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Wed Sep 15, 2004 7:05 am Post subject: |
|
|
boots wrote: | kills wrote: | what does BC means? |
BC = backwards compatible |
ahh thank you
what about my suggestion? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Sep 15, 2004 2:53 pm Post subject: |
|
|
kills wrote: | what about my suggestion? |
Hi kills.
Well, that was basically the same thing I proposed though you managed to come up with a name for the attribute I'm thinking that it is important to keep the name attribute involved.
Unfortunately, we seem to be the only ones who care about this... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 15, 2004 3:27 pm Post subject: |
|
|
Adding a new parameter that changes variable assignment behavior just seems unintuitive and the wrong path to persue, and auto-assigning a var would cause BC problems for sure.
Apparently one of these would have to be compromised. If I had to pick one, I'd say to auto-assign the "name" var, and throw an error if the template var is already assigned. This is a BC issue so we'd just live with it, or add a property that keeps BC (yuck!) |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Wed Sep 15, 2004 3:58 pm Post subject: |
|
|
mohrt wrote: | Adding a new parameter that changes variable assignment behavior just seems unintuitive and the wrong path to persue, and auto-assigning a var would cause BC problems for sure.
Apparently one of these would have to be compromised. If I had to pick one, I'd say to auto-assign the "name" var, and throw an error if the template var is already assigned. This is a BC issue so we'd just live with it, or add a property that keeps BC (yuck!) |
if you assign the var only when another optional attribute ist set, you can save BC.
e.g.
old one which runs because of BC
Code: |
{section name="tr" loop=$Links step=3}
{ $smarty.section.tr.index }
{ $smarty.section.tr.iteration }
{ $smarty.section.tr.last }
{/section}
|
this should work:
Code: |
{section name="tr" this="pointer" loop=$Links step=3}
{ $pointer.index }
{ $pointer.iteration }
{ $pointer.last }
{/section}
|
and this one two
Code: |
{section this="pointer" loop=$Links step=3}
{ $pointer.index }
{ $pointer.iteration }
{ $pointer.last }
{/section}
|
so you only need to assign a var if the "this" attribute is set |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 15, 2004 4:09 pm Post subject: |
|
|
This was my point though, adding an attribute (parameter) to adjust the section internal variable assignment. This adds complexity at the expense of saving BC. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 15, 2004 4:15 pm Post subject: |
|
|
How about this: we introduce $_ as a new type of "internal" var for section and foreach loops (since they seem to be the only thing that needs them):
Code: | {section name="tr" loop=$Links step=3}
{ $_tr.index }
{ $_tr.iteration }
{ $_tr.last }
{/section} |
A good compromise as it has a very low probability for BC collision and we don't add any new attributes to the {section} function. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Sep 15, 2004 4:30 pm Post subject: |
|
|
That occurred to me, but I don't like it, to be honest. Maybe its too Perl-ish Why was %% deprecated?
FWIW, I think I prefer "alias" to "this" ie:
either:
Code: | {section name="foo" alias=true loop=$bar} |
or:
Code: | {section name="foo" alias="foo" loop=$bar} |
or perhaps this is best after all:
Code: | {section alias="foo" loop=$bar} |
The reason I came up with "alias" is because I was considering writing a compiler plugin that worked like assign but did an assign-by-ref:
Code: | {alias var=$smarty.section as=sec} |
so $sec would be a reference to $smarty.section. That way the template designer could put the alias at the top of the template and then would use section as normal, except they can use the aliases as short cuts:
Code: | {alias var=$smarty.section as=sec}
...
{section name="foo" loop=$bar}
{$sec.foo.index}
{/section} |
|
|
Back to top |
|
|