Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Object property assignment within template

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
mattejames
Smarty Rookie


Joined: 15 Jan 2008
Posts: 7

PostPosted: Thu Jan 12, 2012 3:47 pm    Post subject: Object property assignment within template Reply with quote

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


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

PostPosted: Thu Jan 12, 2012 8:56 pm    Post subject: Reply with quote

probably not, but if you have to do that in a template, you are doing it wrong Smile

(ie. do object property assignments in PHP)
Back to top
View user's profile Send private message Visit poster's website
mattejames
Smarty Rookie


Joined: 15 Jan 2008
Posts: 7

PostPosted: Thu Jan 12, 2012 9:35 pm    Post subject: Reply with quote

mohrt wrote:
probably not, but if you have to do that in a template, you are doing it wrong Smile

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


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

PostPosted: Thu Jan 12, 2012 9:40 pm    Post subject: Reply with quote

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


Joined: 15 Jan 2008
Posts: 7

PostPosted: Thu Jan 12, 2012 9:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
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