Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
quote: You can't do it within HTML::Template, period

According to whom?

#!/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;
Produces the following html:
  • a (depth=1)
  • b (depth=1)
    • b1 (depth=2)
    • b2 (depth=2)
  • c (depth=1)
    • c1 (depth=2)
      • c1.1 (depth=3)
  • 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


In reply to Re^2: HTML::Template, pseudo trees and indention. by maverick
in thread HTML::Template, pseudo trees and indention. by BUU

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (8)
    As of 2020-01-17 16:30 GMT
    Find Nodes?
      Voting Booth?