in reply to Re: HTML::Template, pseudo trees and indention.
in thread HTML::Template, pseudo trees and indention.
quote: You can't do it within HTML::Template, period
According to whom?
Produces the following html:#!/usr/bin/perl use strict; use HTML::Template; my $template = " <ul> <TMPL_LOOP tree> <li><TMPL_VAR value> (depth=<TMPL_VAR depth>) <TMPL_LOOP open><ul></TMPL_LOOP> <TMPL_IF close> <TMPL_LOOP close> </li></ul> <TMPL_IF __LAST__></li></TMPL_ +IF> </TMPL_LOOP> <TMPL_ELSE> <TMPL_UNLESS open> </li> </TMPL_UNLESS> </TMPL_IF> </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) { my $delta; if (defined($tree->[$i+1])) { $delta = $tree->[$i+1]->{'depth'} - $tree->[$i]->{'dep +th'}; } else { $delta = - $tree->[$i]->{'depth'}; } if ($delta > 0) { push(@{$tree->[$i]->{'open'}},{}) for (1 .. $delta); } elsif ($delta < 0) { push(@{$tree->[$i]->{'close'}},{}) for ($delta .. -1); } } my $html_template = HTML::Template->new( scalarref => \$template, loop_context_vars => 1); $html_template->param('tree' => $tree); print $html_template->output;
- a (depth=1)
- b (depth=1)
- b1 (depth=2)
- b2 (depth=2)
- c (depth=1)
- c1 (depth=2)
- c1.1 (depth=3)
- c1 (depth=2)
- d (depth=1)
Isn't there some law of comp-sci that says that say any algorithm that can be written recusively, can also be written iteratively? er...or something to that effect.
Update: fixted sprelling erors
/\/\averick
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: HTML::Template, pseudo trees and indention.
by dragonchild (Archbishop) on Jan 10, 2006 at 15:51 UTC | |
by maverick (Curate) on Jan 10, 2006 at 16:03 UTC | |
by dragonchild (Archbishop) on Jan 10, 2006 at 16:47 UTC | |
by maverick (Curate) on Jan 10, 2006 at 16:59 UTC | |
Re^3: HTML::Template, pseudo trees and indention.
by eric256 (Parson) on Jan 10, 2006 at 17:41 UTC | |
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