Re: Another nested array prob
| From: | Andrei Zmievski | Date: | Mon, 26 Feb 2001 15:56:50 +0000 |
| Subject: | Re: Another nested array prob | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-231@lists.php.net to get a copy of this message | ||
On Mon, 26 Feb 2001, graeme@i7.com.au wrote:
> I'm tinkering with Smarty for some templating work. I've got a hellish page
> to enter as templates which involves (for now) some nasty array work. Maybe
> I'm having a brain freeze day :)
>
> If I have an array thus
> $level2["NEWS"] = array("jsname"=>"news",
> "spacing"=> 27 );
> $level2["FINANCE"] = array("jsname"=>"finance",
> "spacing"=>27 );
> $level2["SPORT"] = array("jsname"=>"sport",
> "spacing"=>27 )
>
> Is it possible to access the name of the key? e.g. NAME
> In the code:
> $smarty->assign("tpl_level2", $level2);
>
> And in the template:
> {section name="lvl2" loop=$tpl_level2}
> {$lvl2/tpl_level2.jsname}
> {/section}
>
> I'm guessing this is not possible so some slight restructuring of the array
> is necessary. It's not important, just curious :)
Yes, your array is indexed by string keys, and Smarty uses for() to loop
through it, so it doesn't find anything. Try assigning
array_values($level2) to the template.
> The bigger problem is accessing nested arrays. (apologies for nesting etc)
> $level3 =
> array(
> array(
> "name" => "NEWS",
> "div" => "lvl3news",
> "links" => array(
> array( "text" => "News One", "url" =>
> "http://www.newsone.com/", "target" =>
> "_top"),
> array( "text" => "Nines Two", "url"
> =>
> "http://www.newstwo.com/", "target" =>
> "_top")
> )
> ),
>
> array(
> "name" => "SPORT",
> "div" => "lvl3news",
> "links" => array(
> array( "text" => "NineMSN", "url" =>
> "http://www.ninemsn.com.au/", "target"
> => "_top"),
> array( "text" => "NineMSN", "url" =>
> "http://www.ninemsn.com.au/", "target"
> => "_top")
> )
> )
> );
>
> $smarty->assign("tpl_level3",$level3)
>
> In the template:
> {section name="lvl3" loop=$tpl_level3}
> {$lvl3/tpl_level3.name}<BR>
> {$lvl3/tpl_level3.div}<BR>
> {/section}
>
> I've tried every conceivable combo to access the array inside 'links' but
> to no avail. Can someone give me a clue? :)
>
> I checked out the earlier post on this but I wasn't able to clean much from
> it.
This is currently not possible either. Smarty first indexes by the
section iterations and then by the associative keys (that was a design
decision that had to be made), so you'll need to restructure the array
or assign a separate array.
-Andrei
"The secret of flying is to throw yourself
at the ground, and miss." -- Douglas Adams