View previous topic :: View next topic |
Author |
Message |
Mellou Smarty n00b
Joined: 14 May 2004 Posts: 4
|
Posted: Wed Feb 23, 2005 1:30 pm Post subject: White spaces within array keys? |
|
|
Hello everybody,
it seems that Smarty is not able to handle array fields, if their keys contain white spaces:
Code: | $smarty->assign('array', array('white space' => 'white space')); |
Code: | {$array[white space]} |
It results in "syntax error: unrecognized tag: $array[white space]".
Any ideas how Smarty could parse those array fields, too? I tried different ways (like {$array.'white space'} or {$array['white space']}) but nothing seems to work.
Inside PHP this works just fine:
Code: | echo $array['white space']; |
The documentation does not have any answers either... I would be glad if anyone could help me!
Thanks in advance,
Mellou |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Feb 23, 2005 2:50 pm Post subject: |
|
|
Simple answer: don't use white space in your key names. |
|
Back to top |
|
Aliba Smarty Rookie
Joined: 31 May 2003 Posts: 14
|
Posted: Thu Feb 24, 2005 2:49 am Post subject: |
|
|
For some reason PHP allows white space in an array key. Typically you would not see (nor use) this elsewhere.
Depending on why you may need this functionality you may want use underscores instead of whitespace, and using (in php) the foreach, list you could get back to the space. I imagine you could also do this in Smarty if you need.
I know that once in a while I will use a FORM array for all HTML fields. Then I will use a descriptive name for the key. When $FORM['first_name'] comes in I will use the foreach and list to check each key value pair.
Typically this is useful when you don't want any fields in a form to be empty. In that case I just list out all pairs, check if they are empty and if they are I write a message to an ERRORS array using the key, after the underscores are replaced with spaces. This is well documented in the book "Web application development with PHP 4" by Tobias Ratschiller and Till Gerken. |
|
Back to top |
|
Mellou Smarty n00b
Joined: 14 May 2004 Posts: 4
|
Posted: Thu Feb 24, 2005 12:49 pm Post subject: |
|
|
Hello,
@mohrt: Well, that may be a simple answer, but it does not help me very much. Thanks anyway.
@Aliba: I understand how this could work for me, too. Thanks for your help and the reference you posted. I'll see what I can do...
Greetings,
Mellou |
|
Back to top |
|
moltonel Smarty n00b
Joined: 07 Mar 2005 Posts: 3
|
Posted: Mon Mar 07, 2005 12:52 pm Post subject: |
|
|
Here is a workaround; although not very practical it can get you there without renaming keys :
Code: |
{assign var=mykey value="this key has spaces and %$*^[)& in it"}
{$my.problematic.array.$mykey}
|
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Mar 07, 2005 3:27 pm Post subject: |
|
|
Mellou wrote: |
@mohrt: Well, that may be a simple answer, but it does not help me very much. Thanks anyway.
|
I guess my initial point is to fix the problem, not the symptom. There are probably mixed feelings about this, but IMHO putting spaces (or other special chars) in PHP array keys is only going to introduce more things to debug/maintain further down the development cycle (not just in Smarty.) An array key is much like a variable name and should be treated as such. I'm sure there are people in the other camp (its as string, and strings can have spaces), but Smarty isn't going to cater to it any time soon
One more work around, replace the spaces with underscores right before assigning to Smarty, then at least your templates don't have to deal with them. |
|
Back to top |
|
moltonel Smarty n00b
Joined: 07 Mar 2005 Posts: 3
|
Posted: Mon Mar 07, 2005 6:49 pm Post subject: |
|
|
I can see that there are two camps with this issue; let me try to explain the view of people who want to be able to access any item of a php array:
* first of all, the fact that smarty doesn't accept any key in an array is inconsitent with the "assign a php array to the smarty object, and it becomes available in the template" theory. This confuses a lot of people.
* second, keys with spaces and whatnot are usefull. The keys in php are not mere indexes, they can contain valuable information and may need special formating.
* third, and most annoying to have to work around in smarty, sometimes you have no control over the key name. Either the key was never intended to be used in smarty and is now badly formated for smarty and too widely used for any hope of rename, or the key has a very good technical reason to be formated that way and a rename is not an option.
I've been bitten by all of those, and I my smarty experience would be even happyer if I didn't need to deal with that smarty limitation.
That said, I can very well imagine that fixing the smarty engine to support this would be nontrivial, and that it's a good reason for not putting it in smarty any time soon. |
|
Back to top |
|
Philionel Smarty n00b
Joined: 09 Oct 2013 Posts: 1
|
Posted: Wed Oct 09, 2013 9:24 am Post subject: |
|
|
Its an old thread, but since I was looking for the same thing, this works for me:
{$someArray.{'key with spaces'}} |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Oct 09, 2013 4:47 pm Post subject: |
|
|
Or simpler same like PHP arrays
Code: | {$someArray['key with spaces']} |
|
|
Back to top |
|
|