Filters could be your friend here. Still requires a maximum depth to stop infinite recursion...
In tmpl.tmpl
<TMPL_LOOP kids>
<div style="margin-left:10px;border:1px dotted #555555">
<h3><TMPL_VAR name>.<TMPL_VAR depth></h3>
ho hum...
<TMPL_RECURSE "tmpl.tmpl">Sorry, ran out of steam</TMPL_RE
+CURSE>
</div>
</TMPL_LOOP>
In program
use HTML::Template;
my $MAX_DEPTH = 3;
my $t = HTML::Template->new( filename => "tmpl.tmpl",
die_on_bad_params => 0,
filter => mk_filter($MAX_DEPTH
+));
# populate with recursive data...
$t->param( kids => recursive() );
print "<html><head><title>Wheeee!</title></head><body>";
print $t->output();
print "</body></html>";
#
# create a filter for HTML::Template
#
sub mk_filter
{
my $max = shift;
my $cur = 0;
return sub {
print STDERR "FILTERING:\n",${$_[0]},"\n\n";
1 while ${$_[0]} =~ s#<TMPL_RECURSE\s+?"([^"]+)"\s*>(.*?)</TMP
+L_RECURSE>#do_filter($max,$cur++,$1,$2)#sgei ;
print STDERR "FILTERED:\n",${$_[0]},"\n\n";
};
}
#
# Perform substitution
#
sub do_filter
{
my($max,$cur, $file, $els) = @_;
if($cur < $max) {
open my $f, $file or die $!;
local $/ = undef;
return <$f>;
}
else{
return $els;
}
}
#
# just produce some recursive data...
#
sub recursive
{
my $depth = shift || 0;
my @kids = ();
return [] if $depth > $MAX_DEPTH;
push @kids, { name=> $_, depth=>$depth, kids=> recursive($depth+1)
+ }
foreach (0...2 );
return \@kids;
}
time was, I could move my arms like a bird and...