Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: list of list and HTML::Template

by Ctrl-z (Friar)
on Sep 20, 2005 at 17:20 UTC ( [id://493529]=note: print w/replies, xml ) Need Help??


in reply to list of list and HTML::Template

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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://493529]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-18 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found