Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by maverick (Curate)
on Jan 10, 2006 at 19:42 UTC ( [id://522296]=note: print w/replies, xml ) Need Help??


in reply to Re^3: HTML::Template, pseudo trees and indention.
in thread HTML::Template, pseudo trees and indention.

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

Replies are listed 'Best First'.
Re^5: HTML::Template, pseudo trees and indention.
by eric256 (Parson) on Jan 10, 2006 at 21:03 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://522296]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-19 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found