View previous topic :: View next topic |
Author |
Message |
jdinbrla Smarty n00b
Joined: 13 Feb 2004 Posts: 1
|
Posted: Fri Feb 13, 2004 6:02 pm Post subject: bug re: dashes in keys of associative arrays |
|
|
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
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Feb 13, 2004 7:04 pm Post subject: |
|
|
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 |
|
c960657 Smarty Regular
Joined: 07 May 2003 Posts: 75 Location: Copenhagen, Denmark
|
Posted: Fri Apr 02, 2004 12:49 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 02, 2004 1:10 pm Post subject: |
|
|
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.
I don't see a big problem in allowing people to use äöüāņ etc. in identifiers if they know what they do. |
|
Back to top |
|
c960657 Smarty Regular
Joined: 07 May 2003 Posts: 75 Location: Copenhagen, Denmark
|
Posted: Fri Apr 02, 2004 3:10 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 02, 2004 3:50 pm Post subject: |
|
|
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 |
|
dthought Smarty Regular
Joined: 21 Apr 2003 Posts: 55 Location: Melbourne, Australia
|
Posted: Sun Apr 04, 2004 1:23 pm Post subject: |
|
|
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 |
|
|