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

Cannot assign variable with hyphen in key?

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
kaitlyn2004
Smarty Regular


Joined: 26 Apr 2007
Posts: 47

PostPosted: Thu Feb 17, 2011 4:32 pm    Post subject: Cannot assign variable with hyphen in key? Reply with quote

It appears this does not work:

{$myvar.my-key.my-sub-key="Test"}

Howver,
Code:
$myvar = array(
    'my-key' => array(
        'my-sub-key' => 'Test'
    )
);

is valid...

Bug? Workaround?
Back to top
View user's profile Send private message
kaitlyn2004
Smarty Regular


Joined: 26 Apr 2007
Posts: 47

PostPosted: Thu Feb 17, 2011 4:40 pm    Post subject: Reply with quote

Saw a couple of discussions saying don't use hyphens in keys..

Problem is I want to do something like:

{$meta.http-equiv.content-type="Test..."}

so that in my header it becomes <meta http-equiv="content-type" content="Test..." />

I could use underscores converted to hyphens... extra overhead...
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Feb 17, 2011 4:40 pm    Post subject: Reply with quote

I do believe that dash "-" is not a valid Smarty variable name, and that includes array keys. PHP highly discourages them because they appear as subtraction, and you have to enclose things in braces and quotes to make them work. Bottom line, avoid using dashes in array keys and values, in both PHP and Smarty. Use underscore instead.
Back to top
View user's profile Send private message Visit poster's website
kaitlyn2004
Smarty Regular


Joined: 26 Apr 2007
Posts: 47

PostPosted: Thu Feb 17, 2011 4:40 pm    Post subject: Reply with quote

mohrt wrote:
I do believe that dash "-" is not a valid Smarty variable name, and that includes array keys. PHP highly discourages them because they appear as subtraction, and you have to enclose things in braces and quotes to make them work. Bottom line, avoid using dashes in array keys and values, in both PHP and Smarty. Use underscore instead.


Looks like I had just replied as you did...
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Feb 17, 2011 4:45 pm    Post subject: Reply with quote

You might be able to wrap them in a var (untested):

{$meta = "http-meta"}
{$type = "content-type"}

{$foo.$meta.$type = "foobar"}

But again, you may run into more hoop jumping later. Better off avoiding dashes altogether.
Back to top
View user's profile Send private message Visit poster's website
kaitlyn2004
Smarty Regular


Joined: 26 Apr 2007
Posts: 47

PostPosted: Thu Feb 17, 2011 4:51 pm    Post subject: Reply with quote

Hmm.. this works!

{$meta.{"http-equiv"}.{"content-type"}="Testing!"}
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 -> Smarty 3 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