View previous topic :: View next topic |
Author |
Message |
LividGreen Smarty n00b
Joined: 17 Dec 2007 Posts: 2
|
Posted: Mon Dec 17, 2007 2:20 am Post subject: array in the array in the ... |
|
|
I have:
Code: | $array = array(
'someindex' => 'nextindex',
);
$nextarray = array(
'nextindex' => 'somevalue',
);
|
In Smarty template I want to get $nextarray[$array['someindex']]
The only way I know to do this is:
Code: | {assign var=someindex value=$array.someindex}
{$nextarray.$someindex}
|
... because {$nextarray.$array.someindex} raises an error
Does anybody know another painless way to get $nextarray[$array['someindex']] ?
[/code] |
|
Back to top |
|
scuzzy Smarty Regular
Joined: 31 Aug 2003 Posts: 84
|
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Mon Dec 17, 2007 8:33 am Post subject: |
|
|
When this (or a similar) question comes up Boots usually sends you to search the forums for his "lookup" modifier. Now I tried searching it (out of curiosity) and I needed to google for "smarty_modifier_lookup" to get to some results. When searching for "lookup modifier" in the forums you usually find posts where Boots sends you looking for "lookup modifier" in the forums
Anyway, I found a few versions of the modifier and I'll just post the one I liked most here so maybe it's found more easily in the future
Code: | function smarty_modifier_lookup($value='',$from=null) {
if(is_array($from)) {
if (array_key_exists($value, $from)) {
return $from[$value];
}
} elseif(is_object($from)) {
if(isset($from->{$value})) {
return $from->{$value};
}
}
return '';
} |
Usage:
Code: | {$array.someindex|lookup:$nextarray} |
_________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
LividGreen Smarty n00b
Joined: 17 Dec 2007 Posts: 2
|
Posted: Mon Dec 17, 2007 12:31 pm Post subject: |
|
|
Thank you very much
PS: it's very strange fact that Smarty haven't normal syntax for such usual things... |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Mon Dec 17, 2007 12:45 pm Post subject: |
|
|
Well I guess you might try if
Code: | {$nextarray[$array.someindex]} |
works, I don't know what the [] compile into exactly.
Apart from that Smarty isn't built to access complex data structures but to display content (previoulsy prepared in your calling php file). _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
scuzzy Smarty Regular
Joined: 31 Aug 2003 Posts: 84
|
Posted: Mon Dec 17, 2007 11:29 pm Post subject: |
|
|
Pretty sure square brackets are only good for section loops.
Quote: | {$foo[bar]} <-- syntax only valid in a section loop, see {section} |
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Dec 20, 2007 7:27 pm Post subject: |
|
|
Celeb wrote: | When this (or a similar) question comes up Boots usually sends you to search the forums for his "lookup" modifier. Now I tried searching it (out of curiosity) and I needed to google for "smarty_modifier_lookup" to get to some results. When searching for "lookup modifier" in the forums you usually find posts where Boots sends you looking for "lookup modifier" in the forums |
oh noes! I has a strange loop!
Well, it seems our forum software is letting us down. Our searching feature here is not what it ought be. I just want to remind anyone who thinks of using the lookup modifier that it is meant as an example and so the exposition is rather naive. I'd expect real production code to be more defensive. Further, the lookup modifier (like all modifiers) has a certain runtime cost to it compared to a to strictly lexical expression.
Thanks for doing the leg work here, Celeb |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Dec 20, 2007 7:31 pm Post subject: |
|
|
scuzzy wrote: | Pretty sure square brackets are only good for section loops.
Quote: | {$foo[bar]} <-- syntax only valid in a section loop, see {section} |
|
True, that syntax is only available for section loops; however, Celeb used a different syntax:
Code: | {$nextarray[$array.someindex]} |
Notice that in the former example, a named section is being referenced while in the latter example, a template variable is referenced; however there are shortcoming to the bracket syntax. Only very simple template variable references are acceptable (for example $foo[$bar] and $foo[$bar.baz] etc). Never-the-less, it is sometimes convenient to use the bracket syntax to avoid ambiguities. |
|
Back to top |
|
|