|
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 |
bmpowell Smarty n00b
Joined: 15 Sep 2005 Posts: 1 Location: Morgantown, WV
|
Posted: Thu Sep 15, 2005 2:23 pm Post subject: Periods in Associative Array Keys |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Sep 15, 2005 3:33 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Sep 15, 2005 4:58 pm Post subject: |
|
|
renaming is best but you can also use ugly intermediates:
{assign var=temp value="bad.key"}
{$foo.$temp} |
|
Back to top |
|
orrd Smarty Rookie
Joined: 29 Jun 2010 Posts: 11
|
Posted: Tue Jun 29, 2010 12:13 am Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jun 29, 2010 3:16 pm Post subject: |
|
|
Smarty3 does accept {$foo['bad.key']}.
For Smarty2 the workaround of boots is still valid. We will not change the syntax. |
|
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
|