Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

$this pointer instead of e.g. $smarty.foreach.name.varname
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Sun Sep 12, 2004 1:09 pm    Post subject: $this pointer instead of e.g. $smarty.foreach.name.varname Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Sep 12, 2004 6:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Mon Sep 13, 2004 6:39 am    Post subject: Reply with quote

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 Shocked

i thought about an smarty side solution, because of i think the actual way is realy involved. Embarassed
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Sep 13, 2004 3:14 pm    Post subject: Reply with quote

$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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Sep 13, 2004 5:38 pm    Post subject: Reply with quote

@monte: that's the same one I proposed the other day, so I'm in agreement. Smile 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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Sep 13, 2004 8:16 pm    Post subject: Reply with quote

Yes, $SCRIPT_NAME was kept for BC, it was originally a default global_assign variable.
Back to top
View user's profile Send private message Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Wed Sep 15, 2004 6:14 am    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 15, 2004 6:56 am    Post subject: Reply with quote

kills wrote:
what does BC means?


BC = backwards compatible
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Wed Sep 15, 2004 7:05 am    Post subject: Reply with quote

boots wrote:
kills wrote:
what does BC means?


BC = backwards compatible


ahh thank you Wink

what about my suggestion?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 15, 2004 2:53 pm    Post subject: Reply with quote

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 Smile 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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 15, 2004 3:27 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Wed Sep 15, 2004 3:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 15, 2004 4:09 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 15, 2004 4:15 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 15, 2004 4:30 pm    Post subject: Reply with quote

That occurred to me, but I don't like it, to be honest. Maybe its too Perl-ish Smile 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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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
Protected by Anti-Spam ACP