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

array in the array in the ...

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


Joined: 17 Dec 2007
Posts: 2

PostPosted: Mon Dec 17, 2007 2:20 am    Post subject: array in the array in the ... Reply with quote

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
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Mon Dec 17, 2007 6:05 am    Post subject: Reply with quote

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=12412

Maybe your problem can be solved by my hack in that thread?

{assign var=someindex value=$array.someindex}

{$nextarray.$someindex}
Back to top
View user's profile Send private message Visit poster's website
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Dec 17, 2007 8:33 am    Post subject: Reply with quote

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 Wink

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 Smile
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
View user's profile Send private message
LividGreen
Smarty n00b


Joined: 17 Dec 2007
Posts: 2

PostPosted: Mon Dec 17, 2007 12:31 pm    Post subject: Reply with quote

Thank you very much Smile

PS: it's very strange fact that Smarty haven't normal syntax for such usual things...
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Dec 17, 2007 12:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Mon Dec 17, 2007 11:29 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Dec 20, 2007 7:27 pm    Post subject: Reply with quote

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 Wink


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 Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Dec 20, 2007 7:31 pm    Post subject: Reply with quote

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
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 -> General 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