RE: [SMARTY] evaluate data recursive
| From: | Simon H | Date: | Sat, 31 Jan 2004 15:40:08 +0000 |
| Subject: | RE: [SMARTY] evaluate data recursive | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-10906@lists.php.net to get a copy of this message | ||
> >
> > without any plugins or extensions the only way I know to do
> recursion
> > inside the template is to recursively include a template. Something
> > like:
> >
> > [menu.tpl]
> > {$item->name}
> > {foreach from=$item->childs item=child} {include file="menu.tpl"
> > item=$child} {/foreach}
> >
> >
>
> Thanks for your reply. I guess this would work quite well for
> my purpose, except, that I should know how deep I am in the
> recursion for putting spaces ( ) in front of the links.
>
> Is there a plan to include a Smarty function which could look
> like this:
>
> {recursive from=$variable item=new
> type=preorder|inorder|postorder
> recursive_for=$variable->childs name=my_recursion}
> {smarty.my_recursion.depth} {new->name} {/recursive}
>
> Because recursive datastructres are quite usefull in many
> areas of computer science I think it would be a great feature
> if smarty could include a function for handling them...
>
> Or is such a plugin written easily, I didn't know that,
> because I never had the need to write a smarty-plugin
You could try using Pear::Tree which builds for XML/DB/array nested data
structures.
For DB though I have found that another Pear Class Pear::DB_NestedSet is easier
to use.
http://pear.php.net
Each will output a tree structure with a "level" field in the row data array,
and in the right order. You can then do something like:
foreach ($treeNodes as $nodes){
$indent = str_repeat(" ", $nodes['level']);
$row .= sprintf('%s%s', $indent, $nodes['article_name']);
}
...in basic php. In smarty is just as easy.
Both Pear solutions (I think) support adding and modifying nodes in the tree so
become very useful.
>
> Sincerly, Patrick
--
Simon H