[San-Diego-pm] Recurse through trees

Emile Aben emileaben at gmail.com
Fri Mar 17 06:56:23 PST 2006


Hi,

My main concern was for a better/nicer way of separating logic and
presentation for a Tree::Simple [1] tree object (that in my case
represents the relationship between some filesystems) that I was
passing into template toolkit [2], and where i needed a perl-codeblock
to get the presentation right.

For now I ended up using the same perl-codeblock in my template-toolkit
as I had before (pasted below, for the curious). I Looked into the
Tree::Simple visitor [3] pattern, but that didn't allow me to improve
separation between logic and presentation, it would only move both
logic an presention to the 'visitor' I'd write.

XSLT might allow me to, but AFAICS I'd have to convert my tree to XML
first, then process it. Having 2 separate templating
technologie-thingies (TT and XSLT) together in 1 application doesn't
give me a warm fuzzy feeling.

For myself I've started justifying having code in my template by
calling it 'display logic' (as opposed to 'controller logic'), and
that makes me a lot less unhappy about the way i solved this as I
initially was.

Hope some of this makes sense, I'd be happy to elaborate on a next PM meeting.

emile

[1] http://search.cpan.org/dist/Tree-Simple/lib/Tree/Simple.pm
[2] http://www.template-toolkit.org/
[3] http://search.cpan.org/dist/Tree-Simple/lib/Tree/Simple/Visitor.pm

On 3/15/06, David Romano <david.romano at gmail.com> wrote:
> Emile brought up a problem where he had to recurse through a tree-like
> structure. I just skimmed an article by chromatic on the
> XML::XPathEngine module, and thought Emile *might* find it useful:
> http://xrl.us/kf3b (Link to www.oreillynet.com)
>
> David
> _______________________________________________
> San-Diego-pm mailing list
> San-Diego-pm at pm.org
> http://mail.pm.org/mailman/listinfo/san-diego-pm
>

---code---
[% tree = filelocation.logical_tree('string') %]
[% PERL %]
 my $tree = $stash->get('tree');
 html_node( $tree, '&nbsp;&nbsp;&nbsp;');
 $tree->traverse(sub{
    my($node)=@_;
    html_node( $node, '&nbsp;&nbsp;&nbsp;');
 });

 sub html_node {
    my ($node,$spacer) = @_;
    if ($node->isLeaf) {
        print '<span><div class="filelocation_tree_leave">';
    } else {
        print "<span title='some text about leaf-nodes here (".
$node->getNodeValue() .")'><div class='filelocation_tree'>";
    }
    print $spacer x ($node->getDepth());
    print '<img src="raw?file=L.png" style="padding-right:1px"/>' if
($node->getDepth > -1);
    print $node->getNodeValue();
    print '</div>';
    print '</span>';
 }

[% END %]

---end code---


More information about the San-Diego-pm mailing list