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

bug re: dashes in keys of associative arrays

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
jdinbrla
Smarty n00b


Joined: 13 Feb 2004
Posts: 1

PostPosted: Fri Feb 13, 2004 6:02 pm    Post subject: bug re: dashes in keys of associative arrays Reply with quote

Smarty seems to have a problem with associative array keys that contain dashes. Rather than passing a literal dash, it tries to subtract the second half of the key from the first half of the key, and even takes off a letter

I was wrestling with the logic of my code for 30 minutes before I realized this Razz

my template contains the following text:
<br><small>{$question.help-text}</small>

the output to my browser is the following:
<br><small>0</small>

and the cached template contains the following text:
<br><small><?php echo $this->_tpl_vars['question']['help']-$this->_tpl_vars['ext']; ?></small>

just for reference, here is the template in question: (note: it also has a problem with the {if $question.answer-type == 'multiple'} statement)
[php:1:e93979c6fe]
<form method="post" action="{$action}">
<input name="transaction" type="hidden" value="{$response->questionSetID}">
{foreach from=$response->questions item=question name=outer}
<input name="questions[]" type="hidden" value="{$question.id}">
{if $question.answer-type == 'multiple'}
{assign var="type" value="checkbox"}
{elseif $question.answer-type == 'single'}
{assign var="type" value="radio"}
{/if}
<p>
{$question.text}<br>
{foreach from=$question.choices key=choiceID item=choiceText name=inner}
<label><input type="{$type}" name="q{$question.id}[]" value="{$choiceID}">{$choiceText}</label><br>
{/foreach}
<br><small>{$question.help-text}</small>
</p>
{/foreach}
<input type="submit" value="Submit">
</form>[/php:1:e93979c6fe]
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Feb 13, 2004 7:04 pm    Post subject: Reply with quote

your array keys must consist only of word-characters (letters, digits and the underscore) just like identifiers in php.

if you have more complicated keys, you cannot use them literally, you have to assign them first:
{assign var=key value="help-text"}{$question.$key}

so it's best if you structure your array-keys that they suit smarty.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
c960657
Smarty Regular


Joined: 07 May 2003
Posts: 75
Location: Copenhagen, Denmark

PostPosted: Fri Apr 02, 2004 12:49 pm    Post subject: Reply with quote

messju wrote:
your array keys must consist only of word-characters (letters, digits and the underscore) just like identifiers in php.


Identifiers in PHP may contain characters with ASCII values > 127 (see here). This appears not to be the case with Smarty. I could not find any mention of legal characters in the Smarty documentation.

If I understand Smarty_Compiler.class.php correctly, only characters that match \w with preg_match are allowed. The PHP documentation is vague about exactly which characters this is, but appearently it depends on the current locale.

Should characters with ASCII values > 127 be allowed in Smarty variable names (currently they are not, but for consistency with PHP this should perhaps be changed)? Having the allowed characters depend on the current locale is probaly not a good idea.
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 02, 2004 1:10 pm    Post subject: Reply with quote

okay, then may anlogy to php was wrong.

I don't think enabling 0x7f-0xff is a not good idea. You can get serious fun with multibyte-character-encodings like utf-8 then.

Maybe It's a little dangerous to use \w+ that varies with the locale, but nobody forces you to use more from \w than [a-zA-Z0-9_] (which is always in there, irregardles of the locale, AFAIK).

I always used [a-zA-Z0-9_]+ and never ran out of identifiers, yet. Smile

I don't see a big problem in allowing people to use äöüāņ etc. in identifiers if they know what they do.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
c960657
Smarty Regular


Joined: 07 May 2003
Posts: 75
Location: Copenhagen, Denmark

PostPosted: Fri Apr 02, 2004 3:10 pm    Post subject: Reply with quote

messju wrote:
I don't think enabling 0x7f-0xff is a good idea.

Well, you are probably right.

The reason I want array indexes with non-ASCII characters is that I am converting an XML feed with non-ASCII tag names (*sigh* why do people do that?!) to an array that I access from Smarty. I guess I'll resort to converting non-ASCII characters to underscore.

Thanks for your reply.
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 02, 2004 3:50 pm    Post subject: Reply with quote

hmm, a (probably not so efficient) way would be to access the values via a modifier.

(untested)
[php:1:b17aa4b52b]
function smarty_modifier_array_elem($array, $key) {
return $array[$key];
}
[/php:1:b17aa4b52b]

Code:

{$foo|@array_elem:"key-möpsel-döpsel"}


this should give you $foo['key-möpsel-döpsel'] without the need to do
assign the key to an intermediate variable.

but converting the keys to underscores sounds also reasonable.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dthought
Smarty Regular


Joined: 21 Apr 2003
Posts: 55
Location: Melbourne, Australia

PostPosted: Sun Apr 04, 2004 1:23 pm    Post subject: Reply with quote

Yeah, I can relate to this issue. When querying a system that returns XML tags with dashes in the names, Smarty goes nuts.

You could always preprocess the dashes into underscores in your PHP code though, which seems to be a good workaround (if a little slow).
Back to top
View user's profile Send private message Visit poster's website
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 -> Bugs 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