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

White spaces within 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
Mellou
Smarty n00b


Joined: 14 May 2004
Posts: 4

PostPosted: Wed Feb 23, 2005 1:30 pm    Post subject: White spaces within array keys? Reply with quote

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


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

PostPosted: Wed Feb 23, 2005 2:50 pm    Post subject: Reply with quote

Simple answer: don't use white space in your key names.
Back to top
View user's profile Send private message Visit poster's website
Aliba
Smarty Rookie


Joined: 31 May 2003
Posts: 14

PostPosted: Thu Feb 24, 2005 2:49 am    Post subject: Reply with quote

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
View user's profile Send private message
Mellou
Smarty n00b


Joined: 14 May 2004
Posts: 4

PostPosted: Thu Feb 24, 2005 12:49 pm    Post subject: Reply with quote

Hello,

@mohrt: Well, that may be a simple answer, but it does not help me very much. Confused 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
View user's profile Send private message
moltonel
Smarty n00b


Joined: 07 Mar 2005
Posts: 3

PostPosted: Mon Mar 07, 2005 12:52 pm    Post subject: Reply with quote

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


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

PostPosted: Mon Mar 07, 2005 3:27 pm    Post subject: Reply with quote

Mellou wrote:

@mohrt: Well, that may be a simple answer, but it does not help me very much. Confused 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 Wink

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


Joined: 07 Mar 2005
Posts: 3

PostPosted: Mon Mar 07, 2005 6:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
Philionel
Smarty n00b


Joined: 09 Oct 2013
Posts: 1

PostPosted: Wed Oct 09, 2013 9:24 am    Post subject: Reply with quote

Its an old thread, but since I was looking for the same thing, this works for me:

{$someArray.{'key with spaces'}}
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Oct 09, 2013 4:47 pm    Post subject: Reply with quote

Or simpler same like PHP arrays

Code:
{$someArray['key with spaces']}
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