|
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 |
mattejames Smarty Rookie
Joined: 15 Jan 2008 Posts: 7
|
Posted: Thu Jan 12, 2012 3:47 pm Post subject: Object property assignment within template |
|
|
Hey guys, I'm looking to do the following from within a template:
Code: |
{$person->name = "Tommy Jones"}
or
{$person->department->name = "Welding"}
|
Any chance this is coming in the future? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 12, 2012 8:56 pm Post subject: |
|
|
probably not, but if you have to do that in a template, you are doing it wrong
(ie. do object property assignments in PHP) |
|
Back to top |
|
mattejames Smarty Rookie
Joined: 15 Jan 2008 Posts: 7
|
Posted: Thu Jan 12, 2012 9:35 pm Post subject: |
|
|
mohrt wrote: | probably not, but if you have to do that in a template, you are doing it wrong
(ie. do object property assignments in PHP) |
Hi Mohrt,
The example I gave above was a definitely simplified, and unrealistic example. The point was just to make it more readable. In my real code it looks more like this:
Code: |
{$url = $app->url->clone()}
{$url->query->sort = null}
<form action="{$url}" method="get">
<select onchange="this.form.submit();" name="sort">
<option>Alphabetical</option>
<option>By Date</option>
</select>
</form>
|
Here, I'm manipulating a clone of a URL object representing the REQUEST_URI. I want to keep it all the same, but clear out the 'sort' key on the query string so it doesn't double-post (not that this breaks things, it's just an example).
I could create the URL object and pass it into the template from the controller, but as this variable is purely meant for presentation logic, I don't like that idea. I would imagine this is similar to the reason why you've made assignment available in the template layer at all.
I'm not sure how often this comes up for others, but the more sophisticated my tools become (ie, URL objects, etc), the more likely it has been that this kind of need can arise. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 12, 2012 9:40 pm Post subject: |
|
|
without going into semantics about what goes in templates... does your object not have setter/getter methods? that may be the fix to your situation.
{$url->query->setSort(null)}
That syntax is supported already. |
|
Back to top |
|
mattejames Smarty Rookie
Joined: 15 Jan 2008 Posts: 7
|
Posted: Thu Jan 12, 2012 9:56 pm Post subject: |
|
|
mohrt wrote: | without going into semantics about what goes in templates... does your object not have setter/getter methods? that may be the fix to your situation.
{$url->query->setSort(null)}
That syntax is supported already. |
Yes, in this case I happen to have a system where my object works this way, so this is what I've been doing. I think what I'm trying to avoid is a super-long chain in more complex situations where I reset a bunch of things and make tweaks to both the query object and the URL object.
For example
Code: |
// This feels better to me
{$url = $app->url->clone()}
{$url->query->sort = null}
{$url->path = '/my/path'}
// This feels worse to me because it requires me
// to build my objects in a certain way (setters/getters and chaining)
// to work with a template engine.
{$url = $app->url->clone()->setPath('/my/path')->query->setSort(null)}
|
To be clear, I recognize we're talking about a very small subset of the massive functionality of Smarty. Smarty is incredibly robust and I especially love Smarty 3. Thanks for all your hard work! |
|
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
|