How to implement a tree structure in patTemplate?

A general technical discussion area for patTemplate.
Locked
DaesDemon
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Nov 23, 2005 1:22 pm

How to implement a tree structure in patTemplate?

Post by DaesDemon » Thu Jan 19, 2006 10:49 am

Hello fellows,

I am still struggling with patTemplate Concept and i don't find a way to implement a tree structure.

Suppose i have a php object like this:

class tree
{
    var $root; // Node &
}

class Node
{
    var $child,$next; // Node &
}

What kind of template could i use to display this kind of object?

davidrrm
Joomla! Explorer
Joomla! Explorer
Posts: 251
Joined: Mon Sep 05, 2005 3:50 pm

Re: How to implement a tree structure in patTemplate?

Post by davidrrm » Thu Jan 19, 2006 11:32 am

Perhaps you could sketch a picture of how you want the output to look?

DaesDemon
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Nov 23, 2005 1:22 pm

Re: How to implement a tree structure in patTemplate?

Post by DaesDemon » Thu Jan 19, 2006 11:36 am

I have found this interesting thread in patTemplate Forum:
http://forum.php-tools.net/index.php?t=msg&goto=1342
Last edited by DaesDemon on Thu Jan 19, 2006 11:44 am, edited 1 time in total.

DaesDemon
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Nov 23, 2005 1:22 pm

Re: How to implement a tree structure in patTemplate?

Post by DaesDemon » Thu Jan 19, 2006 11:43 am

@davidrrm

In fact, what i want patTemplate to output is more complex than a tree structure, but it would be a good beggining to start thinking about ;)

For example an output could be:

ROOT:(NODE1,NODE2:(NODE21))

or

ROOT
--NODE1
--NODE2
  --NODE22

Isn't the goal of patTemplate to templating? ;)
I would like to be able to present my tree structure in different ways , that is why i would like to use patTemplate.
Last edited by DaesDemon on Thu Jan 19, 2006 11:46 am, edited 1 time in total.

de
Joomla! Ace
Joomla! Ace
Posts: 1477
Joined: Thu Aug 18, 2005 9:06 am
Contact:

Re: How to implement a tree structure in patTemplate?

Post by de » Thu Jan 19, 2006 11:49 am

I guess the link was lost... but I remember reading that it was not really possible at that time (when I read it)... and there was a "solution/workaround" described which seemed not to really deal with the problem... however, I eventually decided to just split them up into separate sub-templates... something like this:

Code: Select all

<patTemplate:tmpl name="menu_list" whitespace="trim">
<ul>{MENU_ITEMS}</ul>
</patTemplate:tmpl>

<patTemplate:tmpl name="menu_item" whitespace="trim">
<li>{LINK}{SUB_MENU_ITEMS}</li>
</patTemplate:tmpl>
(The recursivion was then done by the PHP code and I give the "menu_item" the a list of the sub menu items redered by "menu_list".... if any)

But maybe someone else found a better solution.

Btw. I do not see any tree structure in the two classes you gave... it's a chained list (which I do not really see a good use for in PHP since arrays are not of a fixed size and I doubt you are constantly adding and removing elements)... but maybe $child was supposed to be $parent? Then it would be both, but I'd still not use a chained list (just makes things more complicate).
Last edited by de on Thu Jan 19, 2006 11:51 am, edited 1 time in total.

DaesDemon
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Nov 23, 2005 1:22 pm

Re: How to implement a tree structure in patTemplate?

Post by DaesDemon » Thu Jan 19, 2006 12:17 pm

de wrote: Btw. I do not see any tree structure in the two classes you gave... it's a chained list (which I do not really see a good use for in PHP since arrays are not of a fixed size and I doubt you are constantly adding and removing elements)... but maybe $child was supposed to be $parent? Then it would be both, but I'd still not use a chained list (just makes things more complicate).
hehe , i have writen that quickly, it is not a tree :( but it is a tree i need :)
Thanks however for infos, i will post back , if i find a way that suit me.
But i am afraid that i will have to manage that as in the last post in the link above
PHP recurse for a html string output and addVar the string to master template . :(

de
Joomla! Ace
Joomla! Ace
Posts: 1477
Joined: Thu Aug 18, 2005 9:06 am
Contact:

Re: How to implement a tree structure in patTemplate?

Post by de » Thu Jan 19, 2006 12:36 pm

I just had a look at the thread again and I think that is the one I saw... but the last post was not there.
This is similar to my solution... but I have to admit that I prefer to not distribute open and close tag throughout different sub templates.
DaesDemon wrote:
de wrote: Btw. I do not see any tree structure in the two classes you gave... it's a chained list (which I do not really see a good use for in PHP since arrays are not of a fixed size and I doubt you are constantly adding and removing elements)... but maybe $child was supposed to be $parent? Then it would be both, but I'd still not use a chained list (just makes things more complicate).
hehe , i have writen that quickly, it is not a tree :( but it is a tree i need :)
You may still want to consider using just arrays, actually one: $children. It seems simpler to me and I am pretty sure you can represent the same tree with it you need... and then you could use the same code for similar problems and do not have to rethink just because this time it is slightly different. Well, just my suggestion ;-)

DaesDemon
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Nov 23, 2005 1:22 pm

Re: How to implement a tree structure in patTemplate?

Post by DaesDemon » Thu Jan 19, 2006 12:55 pm

hehe,
my classes are something like this

class Table
{
    var $fields=array();
    many functions theres
    function render()
    {
        foreach $fields as $field $field->_render()
    }
}

/* generic fields
class Field
{
    var label;
    many functions
    funciton _render() {};
}

class specificField extends genericFields
{
    var $specifVars;
    function _render();
}

// This is where i need the tree structure
class SubTableField extends genericFields
{
    var $table; // Table
    _render()
    {
        table->render();
    }
}

So i am struggling to use patTemplate in a structure like this.
Don't think arrays are a viable solution for me, except if i reconsider everything from the beginning , and that, i cannot do :)
Perhaps in version 2 ;)

What i had wanted to have is something like a template file with tmpl:Table tmpl:specificField tmpl:tableField ... defined and that would have change all my presentation.
But i guess i will have to do a part of the work myself, to make the things easy for patTemplate.

de
Joomla! Ace
Joomla! Ace
Posts: 1477
Joined: Thu Aug 18, 2005 9:06 am
Contact:

Re: How to implement a tree structure in patTemplate?

Post by de » Thu Jan 19, 2006 1:12 pm

Honestly I think your "new" classes is using an array... but instead it is named $fields. :-)

You may just adopt what is described in the last post of the other thread... I think that works fine and probably better in your case because some fields may not use patTemplate and you'd either use output buffering or decide to return the output instead. I have to admit that I tend to return the output (so I am just appending strings)... but I cannot tell which is better... they have both pros and cons.

DaesDemon
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Nov 23, 2005 1:22 pm

Re: How to implement a tree structure in patTemplate?

Post by DaesDemon » Thu Jan 19, 2006 1:52 pm

yes table use an array of classes Field but those fields may contain table, so it is why i call it a tree structure  ;D

Table
--Field1
--Field2
  --Table
      --Field...
      --Field...
        --Table and so on

What do you call tree?

I am currently implementing the method you talked.
I'll just finish that and post back about the results and problems i will encounter;)

de
Joomla! Ace
Joomla! Ace
Posts: 1477
Joined: Thu Aug 18, 2005 9:06 am
Contact:

Re: How to implement a tree structure in patTemplate?

Post by de » Thu Jan 19, 2006 2:12 pm

Yes, that is a tree structure... and I have no complain with it (only some naming maybe)...
I just don't see any similarity to your original structure - that was the one I would not advice to heh.
So don't worry... everthing is fine ;-)

DaesDemon
Joomla! Apprentice
Joomla! Apprentice
Posts: 37
Joined: Wed Nov 23, 2005 1:22 pm

Re: How to implement a tree structure in patTemplate?

Post by DaesDemon » Sat Jan 21, 2006 2:48 pm

So these are my first result for a tree structure in patTemplate.
The main problem is that you cannot use two times one template with different rows.
It means that there is no instance of template, only templates.

One of the solution i found is this one:
It is based on the fact that you can buffer results of the childs template before using the parent template, recursivly, so this way, the childs are parsed first and the parent clear the template before using it.

Suppose we get two classes describing a tree structure:
aTree is just a container containing the root aNode
aNode is the real tree structure.
Each of these class getting their own template:
This is an example of tabular display.

Code: Select all



    
        {LABEL}
        {ROOT}
    





    
        {LEVEL}
        {DATA}
        
        
        {CHILD}
        
        
    


You can use this code PHP to fill them.

Code: Select all

class aTree
{
    var $root;
    var $label;
    function aTree()
    {
        $this->label = "Label";
        $this->root = new aNode("Root");
    }
    
    function output(&$tmpl)
    {
        $tmpl->clearTemplate("tree");
        $tmpl->addVar('tree','LABEL',$this->label);
        $tmpl->addVar('tree','ROOT',$this->root->output($tmpl,0));
        return $tmpl->getParsedTemplate('tree');
    }
}

class aNode
{
    var $childs = array();
    var $data;
    function aNode($data)
    {
        $this->data = $data;
    }
    
    function output(&$tmpl,$level=0)
    {
        $output = array();
        // Buffering of the child template parsing
        foreach ($this->childs as $child) 
        {
            // Recursion
            $output[] = $child->output($tmpl,$level+1);            
        }
        // Main template using after clearing
        $tmpl->clearTemplate("node");
        $tmpl->clearTemplate("childs");
        $tmpl->addVar("node","data",$this->data);
        $tmpl->addVar("node","level",$level);
        // adding the childs parse results
        $tmpl->addVar("childs","child",$output);
        // return this node with childs results
        return $tmpl->getParsedTemplate("node");
    }
}
This code can be used like this:

Code: Select all

        $tmpl = // get your template there
        $tmpl->readTemplatesFromFile('tree.tmpl.html');

        // Create an example tree
        $tree = new aTree();
        $tree->root->childs[0] = new aNode("Child1");
        $tree->root->childs[0]->childs[0] = new aNode("Child11");
        $tree->root->childs[0]->childs[1] = new aNode("Child12");
        $tree->root->childs[0]->childs[2] = new aNode("Child13");
        $tree->root->childs[1] = new aNode("Child2");
        $tree->root->childs[2] = new aNode("Child3");

        // output it with the template
        $ret = $tree->output($tmpl);
        // there you get the html of the complete tree output
        return $ret;
On thing interesting is that we could expands the aNode generic class with more specific class.
In this case we could use more specific template only by changing "node" to for example "image-file" or "mp3-file" and display thing differently.

I will continue investigate others method and post others result if i found some.

Hope this will help some tree solution seeking soul ;)
Last edited by DaesDemon on Sat Jan 21, 2006 2:57 pm, edited 1 time in total.


Locked

Return to “patTemplate”