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

Periods in Associative Array Keys

 
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
bmpowell
Smarty n00b


Joined: 15 Sep 2005
Posts: 1
Location: Morgantown, WV

PostPosted: Thu Sep 15, 2005 2:23 pm    Post subject: Periods in Associative Array Keys Reply with quote

I'm working on a fairly large CMS project with Smarty as the output templating engine. We have a lot of queries involving multitable joins, and sometimes we end up with duplicate field names between the tables.

I have my PHP code so that it returns the results in an array keyed on table.field, where there is actually a period as a separator. When I'm writing my Smarty templates, how can I tell Smarty that 'table.field' is actually the key for one array and not a pair of nested arrays? All my attempts have either resulting in PHP errors, Smarty just appending the 'table.field' part as text to the end of the array, or it still thinking there are nested arrays.

Any help would be appreciated. I'd prefer to continue using periods in my array keys for simplicity, but I know I might have to change to something else.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Sep 15, 2005 3:33 pm    Post subject: Reply with quote

Short answer, don't use periods in key names, at least not for template variables. Rename foo.bar to foo_bar. There may be hoops you can jump through to get around this but generally speaking, you're better off avoiding them.
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: Thu Sep 15, 2005 4:58 pm    Post subject: Reply with quote

renaming is best but you can also use ugly intermediates:

{assign var=temp value="bad.key"}
{$foo.$temp}
Back to top
View user's profile Send private message
orrd
Smarty Rookie


Joined: 29 Jun 2010
Posts: 11

PostPosted: Tue Jun 29, 2010 12:13 am    Post subject: Reply with quote

I'm wondering if there is a better fix for this that has been developed over the last 5 years since this was originally posted?

It seems that a good syntax would be {$foo[bad.key]}. Another alternative syntax could be {$foo.bad\.key}. But those don't seem to work either (at least in my Smarty 2.x installation).
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Jun 29, 2010 3:16 pm    Post subject: Reply with quote

Smarty3 does accept {$foo['bad.key']}.

For Smarty2 the workaround of boots is still valid. We will not change the syntax.
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