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

Assigning arrays to variables using {assign ... } tpl func.

 
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 -> General
View previous topic :: View next topic  
Author Message
Knaverjeck
Smarty n00b


Joined: 15 Oct 2003
Posts: 3

PostPosted: Wed Oct 15, 2003 2:55 pm    Post subject: Assigning arrays to variables using {assign ... } tpl func. Reply with quote

Is it possible? Is there a way around it?

I have a foreach loop in which the keys of an array are extracted using array_keys(). In order to optimise this a little, I want to evaluate the array_keys() function outside of the foreach loop and assign it to a template variables. Something like this:

{assign var="idList" assign=array_keys($groups)}
{foreach from=$users item="user"}
{if in_array($user.groupID, $idList)}yes, you're a member{/if}
{/foreach}

But all that gets assigned to $idList is "array_keys(Array.groups)"

Can anyone suggest what I'm doing wring here, or whether smarty supports this at all (if not, how can I get around it)?

thanks
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Oct 15, 2003 3:18 pm    Post subject: Reply with quote

One way around it, do your array manipulation in PHP, then assign it to smarty in a structure that is easy to loop over and display. Always try to have your data prepared for smarty so once you're in the template, minimal effort is required to display it.
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 Oct 15, 2003 10:37 pm    Post subject: Reply with quote

mohrt wrote:
Always try to have your data prepared for smarty so once you're in the template, minimal effort is required to display it.

++ on that!

--- my two cents:
Folks should be reasonable in their expectations of Smarty. It is very tempting to move all of your code into Smarty templates, but that is the wrong way to go. There are tricks to manipulating data in Smarty, but they are difficult and hard to maintain. Remember, the template code is not meant to implement your application logic. I'm sure many are trying to build custom "controls" using Smarty--but again, the code portion of the control should not be done in template. Also remember the target audience for templates! The KISS principle should be your guiding beacon.

Templates are for data OUTPUT. If you need to produce or prepare data, do that in code, not in template. If you DO, prepare data in template, then DO ONLY THAT in your template--do not both prepare data and produce output in the same template. I know people refer to Smarty's abstraction as "template from code" but the abstraction is more aptly "data production from data output".


Last edited by boots on Thu Oct 16, 2003 7:15 pm; edited 1 time in total
Back to top
View user's profile Send private message
Knaverjeck
Smarty n00b


Joined: 15 Oct 2003
Posts: 3

PostPosted: Thu Oct 16, 2003 8:34 am    Post subject: Reply with quote

Thanks for your replies guys. Perhaps I have moved too much into the template itself, but I feel reluctant to create 'filler' variables (such as one to hold the keys of the $group array I mentioned first of all). In my mind this is altering the PHP code to fit around the template, rather than the template just displaying whatever I pass to it in a way that it wants to. i.e. "I need to create this keys array because the template requires it" - that immediately crushes the code/content separation idea.

I'm not talking about moving specific conditional statements that require the template to know exactly what data it's being passed (e.g. {if $userID>=1 && $username=='bob'}), but instead simply restructuring the data that it's got into a more convenient form for display purposes. I don't think this is expecting too much from Smarty - why are these data manipulations "difficult and hard to maintain"?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Oct 16, 2003 5:45 pm    Post subject: Reply with quote

This is how to fix your code example, by-the-way: (untested)

{assign var="idList" assign=$groups|@array_keys}

PHP functions are allowed as modifiers (when security is false) but PHP functions are allowed as functions only in {if} statements. The @ tells Smarty to treat the input in the modifier as an array.

I understand your argument, but I don't particularly agree Smile One thing to consider is that you are doing validation in your template -- I don't see that as being overly related to output, but instead a function of your application. It is true that there are fine lines and grey areas, but in this case, perhaps you may want to consider another layer in your application to get the separation you look for. ....or not, suit yourself!

Cheers,


Last edited by boots on Thu Oct 16, 2003 7:05 pm; edited 1 time in total
Back to top
View user's profile Send private message
Knaverjeck
Smarty n00b


Joined: 15 Oct 2003
Posts: 3

PostPosted: Thu Oct 16, 2003 6:52 pm    Post subject: Reply with quote

thanks boots, i'll give that a try, and try to colour in some of those grey areas whilst I'm at it Smile
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 -> General 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