Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: list of list and HTML::Template

by Ctrl-z (Friar)
on Sep 20, 2005 at 17:20 UTC ( #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
Node Status?
node history
Node Type: note [id://493529]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2020-05-27 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (152 votes). Check out past polls.

    Notices?