Discussion:
[SMARTY] evaluate data recursive
Patrick Pletscher
21 years ago
Permalink
Hi,

I have created a PHP script which creates a tree out of an XML file
which represents the navigation of my website.

Here the datastructre:

class Node {
var $name;
var $link;
var $id;
var $childs = array();
}

I'd like now to show this navigation in the smarty style (of course I
could do a simple recursive echo in my PHP script), but I wanna do it
nicely. But how do so? I search a way to handle recursive datastructes
in the way of {foreach ...} {/foreach}...

Do I have to make a plugin or is there an easy solution for this? How do
you make similar things in your projects?

Thanks a lot in advance, sincerly

Patrick
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
messju mohr
21 years ago
Permalink
Hello,

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}
...
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Patrick Pletscher
21 years ago
Permalink
Hello,
Post by messju mohr
Hello,
without any plugins or extensions the only way I know to do recursion
inside the template is to recursively include a template. Something
[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

Sincerly, Patrick
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Simon H
21 years ago
Permalink
...
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.
Post by messju mohr
Sincerly, Patrick
--
Simon H
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Gabriel Birke
21 years ago
Permalink
Hello,

You can easily get the recursion depth if you modify the recursion like
this (only works with Smarty 2.6 because of the math):

[menu.tpl]
{$item->name|indent:$depth*4:" "}
{foreach from=$item->childs item=child}
{include file="menu.tpl" item=$child depth=$depth+1}
{/foreach}

I haven't tested this but you get the principle ...


With best regards

Gabriel Birke
--
KONTOR4_Neue Medien
Plathnerstraße 5
30175 Hannover
Fax: +49 51184 48 98 99
mailto:***@kontor4.de
http://www.kontor4.de
...
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...