RE : [SMARTY] Recursing through an array

From: Date: Thu, 24 Jul 2003 18:53:41 +0000
Subject: RE : [SMARTY] Recursing through an array
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-9961@lists.php.net to get a copy of this message
Thanks for you help, but I don't think this is the solution I was after. I read a reply from Monte on a similar vein...here it is: Re: [SMARTY] Recursion, include, and {section} - Tue 30/07/2002 ---------------------------------------------------------------- An alternate method, do the recursion in PHP and assemble an array to assign to the template. Give each row a depth value. Use one {section} loop in Smarty and use the depth to figure out the left justification for each line. I use this method because it lets the template handle presentation of data and not bother with how it's related to itself. Monte ---------------------------------------------------------------- Do you have any examples of this Monte? I still havent got this one fixed! I suppose my problem is not so much recursion. I have a 1D array of (id,parent) that I need to transform into an indented tree type layout, based on the parent<->id relationships. I would have thought that I could structure this data within the template rather than having to use a function (I may use a function to generate the array of data), but I can't see a way to do it. Is there a best way to do this? What is the best way to receive data into a template to build tree type layouts? (By best I mean fastest, but maintainable). Thanks to anyone who can help!! -- Simon H > -----Original Message----- > From: Christopher Rousseau > Sent: 22 July 2003 09:31 > To: 'Simon H'; 'Smarty-General' > Subject: [SMARTY] RE : [SMARTY] Recursing through an array > Sensitivity: Confidential > > > I got the same problem recently. > The only solution I found was to use recursivity to build my menu. > > I copy the post I done for a smarty plugin who add recursivity to > smarty. > > It's better to cash the result to have better performance. > > ************************************************************** > ********** > **** > ********************* COPY START > ******************************************* > > Hello, > > I was encounter a small limite using smarty : recursivity > > I have this type of class : > > Class X { > getChildren() { > // Some code to create a list of children > return $arrayOfChildren ; // Array of Object > instanciate from class X (again, and again, and again,…) > } > } > > I was using it for the creation of infinitly recursive menu. > > But I was not able to write my recurive menu using Smarty. It’s why I > have create this small plugin : > > -------------------------------------------------------------- > ---------- > --- > > <?php > > /* > * Smarty plugin > * ------------------------------------------------------------- > * Author : Christopher Rousseau <christopher.rousseau@wanadoo.fr> > * Type: function > * Name: recursive > * Purpose: execute another smarty template inside a template smarty > * Parameters: > * template: template name > * [param] : all else parameter will be assigned to > * the new smarty instance > * ------------------------------------------------------------- > */ > function smarty_function_recursive($params, &$smarty) > { > if > (!array_key_exists('template',$params)||$params['template']==Null) { > $smarty->trigger_error("assign: missing 'template' > parameter"); > return; > } > > $recursiveSmarty = new Smarty(); > $recursiveSmarty->template_dir = $smarty->template_dir; > $recursiveSmarty->compile_dir = $smarty->compile_dir; > > foreach($params as $key => $value) { // we assign all > else parameter to the new Smarty > if ($key!='template') > $recursiveSmarty->assign($key, $value); > } > return $recursiveSmarty->fetch("$params[template]"); > } > > ?> > > This plugin instanciate a new Smarty with the same configuration. > > I’m currently use it like that : > > I assign my root menu in $list variable, $list is an array of object X > > Root.tpl : > > -------------------------------------------------------------- > ---------- > --- > > > <ul> > {foreach from=$list item=menu} > {recursive template='recursive.tpl' menu=$menu} > {/foreach} > </ul> > > -------------------------------------------------------------- > ---------- > --- > > recursive.tpl : > <li> > {$menu->getField('name')} > <ul> > {foreach from=$menu ->getChildren() item=children} > {recursive template='recursive.tpl' menu=$children} > {/foreach} > </ul> > </li> > > -------------------------------------------------------------- > ---------- > --- > > I think I have forget some configuration to pass thru the recursive > Smarty (like cache configuration). But those parameter was > anough for me > (I cache the root template). > > > Hop it can be usefull for some of you, > Bye. > Christopher Rousseau > ************************************************************** > ********** > ************************ COPY END > ********************************************** > > -----Message d'origine----- > De : Simon H [mailto:simon.hamilton@ntlworld.com] > Envoyé : lundi 21 juillet 2003 23:18 > À : Smarty-General > Objet : [SMARTY] Recursing through an array > Critère de diffusion : Confidentiel > > Hello there > > I've been working with Pear::Tree to generate a tree array > which I would > then like to recurse through and indent for every child of a parent > throughout the depth of the tree. > > The example array (shortened) to build the tree is: > > Array ( > [0] => Array ( > [0] => Array ( > [id] => 2 > [parentId] => 1 > ) > [1] => Array ( > [id] => 5 > [parentId] => 2 > ) > [2] => Array ( > [id] => 3 > [parentId] => 2 > ) > [3] => Array ( > [id] => 4 > [parentId] => 3 > ) > [4] => Array ( > [id] => 9 > [parentId] => 4 > ) > [5] => Array ( > [id] => 6 > [parentId] => 3 > ) > [6] => Array ( > [id] => 7 > [parentId] => 3 > ) > [7] => Array ( > [id] => 8 > [parentId] => 7 > ) > ) > ) > > In my template I have no idea (have even tried compiler functions and > templates suggested on the forum) how to recurse through this array > using the parent ID to get something like this (dynamically created of > course): > > <ul> > <li> > [id] => 2 > [parentId] => 1 > <ul> > <li> > [id] => 5 > [parentId] => 2 > </li> > <li> > [id] => 3 > [parentId] => 2 > </li> > <li> > <ul> > <li> > [id] => 4 > [parentId] => 3 > </li> > <li> > <ul> > <li> > [id] => 9 > [parentId] => 4 > </li> > </ul> > </li> > <li> > [id] => 6 > [parentId] => 3 > </li> > <li> > [id] => 7 > [parentId] => 3 > </li> > <li> > <ul> > <li> > [id] => 8 > [parentId] => 4 > </li> > </ul> > </li> > </ul> > </li> > </ul> > </li> > </ul> > > What I'm after is something that can be totally dynamic in terms of > depth...Any ideas anyone? > > This is doin' my head in! > > Thanks in advance! > > -- > Simon H > > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

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