Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: HTML::Template, pseudo trees and indention.

by eric256 (Parson)
on Jan 10, 2006 at 17:41 UTC ( #522256=note: print w/ replies, xml ) Need Help??


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


Comment on Re^3: HTML::Template, pseudo trees and indention.
Download Code
Re^4: HTML::Template, pseudo trees and indention.
by maverick (Curate) on Jan 10, 2006 at 19:42 UTC
    On first glance, the subtle difference is that mine generates all the "optional" closing "li"s. I'm actually using this to generate collapsable trees in a web based app and some of the more drain bamaged *cough* IE *cough* browsers yacked if the html wasn't "well formed".

    /\/\averick

      Ahh. didn't realize how they had to nest. Here is a slightly updated version that passes W3C validation. I assume/hope that means IE will like it.

      #!/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_UNLESS up></li></TMPL_UNLESS> <TMPL_LOOP up><ul></TMPL_LOOP> <TMPL_LOOP down></ul></li></TMPL_LOOP> </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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-10-21 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (98 votes), past polls