Re: [SMARTY] indirect indexing (was: nested foreach loop)

From: Date: Thu, 11 Apr 2002 16:27:28 +0000
Subject: Re: [SMARTY] indirect indexing (was: nested foreach loop)
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-3629@lists.php.net to get a copy of this message
On Thu, 11 Apr 2002, Robert V. Zwink wrote: > The idea is that I have two arrays, one array contains a list of categories, > the other array contains a list of subcategories. The category array is > setup like this > array("3digitID"=>"title") > > and the subcategory array is setup like this > array("3digitID"=>array("4digitID"=>"title")) > > This way I can use Smarty to traverse the category array, then compare the > current 3digitID to the first element in subcategory array, and display the > subcategories. Its seems like I could by pass the {if} conditional if it > were possible to specify which element of a multidimensional array to > foreach. > > My question is how do I specify an element of a multidimensional array to > foreach? I want to be able to do something like this: > > {foreach name=subcategories_left item=subcategory > key=subcat_coordinate_group from=$subcategories_left[$other_foreach_var]} > > Where $other_foreach_var == the "key" from the current parent foreach, and > $subcategories_left is a multidimensional array. Hi, This has been discussed in the past, but what stopped it from going forward is the difficult of parsing recursive variable references using regular expressions. However, your post served as a tipping point, and I now added ability to index arrays and access object properties indirectly via simple variables. Example: /* php script */ $tpl->assign("level1" => array("123" => "one-two-three", "456" => "four-five-six")); $tpl->assign("level2" => "123"); $tpl->assign("complex" => "123456"); {* template *} word: {$level1.$level2} {* or even *} {assign var="simple" value=$complex|truncate:3:"":yes} word: {$level1.$simple} Note that you can't use modifiers or index the indexing variable itself. It has to be a simple template variable. If you want something more complicated, use {assign} as shown above. -Andrei "You choose to do the bad things in your life; the good ones come and drag you along with them." - Michael Marshall Smith

« previous php.smarty.general (#3629) next »