I would use a recursive subroutine to walk the tree and collect the output as you go. Maybe something like this will help you (depth traversal):
use HTML::Template;
my $thingy = [
{
name => 'foo',
id => 1,
children => [
{
name => 'bar',
id => 2,
children => [
{
name => 'baz',
id => 3,
},
{
name => 'qux',
id => 4,
},
],
},
{
name => 'ducks',
id => 5,
},
],
},
{
name => 'last',
id => 6,
},
];
our $tmpl = HTML::Template->new(
filehandle => \*DATA,
die_on_bad_params => 0,
);
our $output;
our $tab_loop = [];
recurse($thingy);
print $output;
sub recurse {
my $thingy = shift;
for (@$thingy) {
$tmpl->param(tab_loop => $tab_loop, %$_);
$output .= $tmpl->output;
push @$tab_loop, { tab => "\t" };
recurse($_->{children}) if ref $_->{children};
pop @$tab_loop;
}
}
__DATA__
<tmpl_loop tab_loop><tmpl_var tab></tmpl_loop><tmpl_var id> - <tmpl_va
+r name>
I'd be tempted to attack this problem with Template instead.
|