[San-Diego-pm] Recurse through trees
Emile Aben
emileaben at gmail.com
Fri Mar 17 15:30:01 PST 2006
> [% spacer = " " %]
> [% MACRO html_node_visitor(node) BLOCK %]
> [% IF node.isLeaf %]
> ... etc etc
> [% ELSE %]
> ... etc etc
> [% END %]
> [% GET spacer FOR 1..node.getDepth %]
> [% node.getNodeValue %] etc etc
> [% END %]
> [% tree.traverse(html_node_visitor) %]
>
> A "MACRO" is a code block!
Thanks, that looks a lot better then the code I had, I now have this code:
[% tree = filelocation.logical_tree('string') %]
[% spacer = ' ' %]
[% MACRO html_node_visitor(node) BLOCK %]
[% IF node.isLeaf %]
<span><div class="filelocation_tree_leave">
[% ELSE %]
<span title='blah blah blah ( [% node.getNodeValue %] )'><div
class="filelocation_tree">
[% END %]
[% spacer.repeat(node.getDepth) %]
[% IF node.getDepth > -1 %]<img src="raw?file=L.png"
style="padding-right:1px"/>[% END %]
[% node.getNodeValue %]
</div>
</span>
[% END %]
[% html_node_visitor(tree) %] <!-- THIS WORKS FINE -->
[% tree.traverse( html_node_visitor ) %] <!-- ERROR -->
Unfortunately I still have a little problem in that the template
toolkit 'stash'
(both the regular, and the experimental Template::Stash::Context)
apparently does some stuff to the code block, so Tree::Simple doesn't
recognize 'html_node_visitor' as a code block, after defining the
macro it works fine like this:
[% html_node_visitor(tree) %]
but dies pm me on:
[% tree.traverse( html_node_visitor ) %]
(die msg:
undef error - Incorrect Object Type : traversal function is not a
function at /usr/local/lib/perl5/site_perl/5.8.7/Tree/Simple.pm line
471.
)
Doing a
[% html_node_visitor.ref %]
prints 'CODE' (which is what I expected).
but datadumping $stash->get('html_node_visitor') finds this is an 'undef'?
Looked a little at the guts of Template::Toolkit, but can't seem to
wrap my brain around the problem right now.
[% html_node_visitor(tree) %]
is translated to:
print $stash->get(['html_node_visitor', [ $stash->get('tree') ]]);
while the nonworking:
[% tree.traverse( html_node_visitor ) %]
is translated to:
print $stash->get(['tree', 0, 'traverse', [
$stash->get('html_node_visitor') ]]);
Before I dig deeper into the guts of Template::Toolkit I'd like to
know if anybody has any idea what I'm doing wrong (or template toolkit
is doing wrong, although that is less likely)?
emile
More information about the San-Diego-pm
mailing list