|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Guardian OfThe Blind Smarty n00b
Joined: 10 Dec 2005 Posts: 1
|
Posted: Sat Dec 10, 2005 12:31 am Post subject: Nested square brackets problem and a solution |
|
|
Smarty can not compile (as far as I know) "nested brackets":
Code: |
{$array1[$array2[section].someId].SomeVar}
|
So I made a workaround that doesn't use {assign}:
Code: |
{$array1|@accessArray:$array2[section].someId:"SomeVar"}
function smarty_modifier_accessArray($arr,$elem,$name) {
return $arr[$elem][$name];
}
|
Any better ideas? _________________ http://www.putovanja.info |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Dec 10, 2005 1:47 am Post subject: |
|
|
Try {foreach} instead of {section} so you can do something like:
{$array1[$item.someId].SomeVar}
I wrote a very similar plugin except it works a bit different: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=3806
You can probably extend it to have the section as an optional last parameter which, if passed, would do cause a 3d lookup. The main difference from yours is that the field name is passed first and the array second -- that way you don't need the @ syntax.
{'SomeVar'|lookup:$array1:$array2[section].someId}
Here it is extended to support that. Of course, it is only untested sample code
[php:1:7190f4e7df]<?php
function smarty_modifier_lookup($value='', $from=array(), $section=null)
{
if (isset($section)) {
if (array_key_exists($section, $from) && array_key_exists($value, $from[$section])) {
return $from[$value];
} else if (array_key_exists($value, $from)) {
return $from[$value];
}
return '';
}
?>
[/php:1:7190f4e7df]
Still, the foreach option looks best to me. |
|
Back to top |
|
|
|
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
|