in reply to Re^2: HTML::Template, pseudo trees and indention.
in thread HTML::Template, pseudo trees and indention.
Here is a bit simpler version. I'm not sure this doesn't miss some special case you where planning on though. It just makes an up or a down loop to raise or level the level as needed.
#!/usr/bin/perl use strict; use HTML::Template; use Data::Dumper; my $template = " <ul> <TMPL_LOOP tree> <li><TMPL_VAR value> (depth=<TMPL_VAR depth>) <TMPL_LOOP up><ul></TMPL_LOOP> <TMPL_LOOP down></ul></TMPL_LOOP> </li> </TMPL_LOOP> </ul> "; my $tree = [ { 'value' => 'a', 'depth' => 1 }, { 'value' => 'b', 'depth' => 1 }, { 'value' => 'b1', 'depth' => 2 }, { 'value' => 'b2', 'depth' => 2 }, { 'value' => 'c', 'depth' => 1 }, { 'value' => 'c1', 'depth' => 2 }, { 'value' => 'c1.1', 'depth' => 3 }, { 'value' => 'd', 'depth' => 1 } ]; for my $i (0 .. $#$tree) { if (defined $tree->[$i+1]) { my $diff = $tree->[$i+1]->{depth} - $tree->[$i]->{depth}; if ($diff > 0) { $tree->[$i]->{up} = [({}) x $diff]; } elsif ( $diff < 0 ) { $tree->[$i]->{down} = [({}) x -$diff]; } } } my $html_template = HTML::Template->new( scalarref => \$template, loop_context_vars => 1); $html_template->param('tree' => $tree); print $html_template->output;
___________
Eric Hodges
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: HTML::Template, pseudo trees and indention.
by maverick (Curate) on Jan 10, 2006 at 19:42 UTC | |
by eric256 (Parson) on Jan 10, 2006 at 21:03 UTC |
In Section
Seekers of Perl Wisdom