RE : [SMARTY] Recursing through an array
| From: | Christopher Rousseau | Date: | Tue, 22 Jul 2003 08:30:45 +0000 |
| Subject: | RE : [SMARTY] Recursing through an array | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-9938@lists.php.net to get a copy of this message | ||
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. Its 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.
Im 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