Very nice. If you don't mind, i'd like to add how to do this with HTML::Mason for
posterity, contrast/compare, etc.
Calling component (nodes.html)
<%init>
use strict;
use warnings;
my @nodes = (
{name => "one"},
{name => "two", children => [
{name => "red"},
{name => "green"},
] },
{name => "three", children => [
{name => "blah", children => [
{name => "yakko" },
{name => "wacko" },
{name => "dot" },
] },
] },
);
$m->comp( 'nodes.mas', nodes => \@nodes );
</%init>
and nodes.mas
<%args>
@nodes
</%args>
<ul>
% foreach my $i (@nodes) {
<li><% $i->{name} %></li>
% if (ref( $i->{children} ) eq 'ARRAY') {
% $m->comp( 'nodes.mas', nodes => $i->{children} );
% }
% }
</ul>
I added a check to ref() for safety.
|