Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Recursively display nested lists with Template Toolkit

by friedo (Prior)
on Aug 28, 2006 at 22:01 UTC ( #570059=snippet: print w/ replies, xml ) Need Help??

Description: Template Toolkit does not allow recursively including template files, but you can use a VIEW to recurse over a data structure. VIEWs are still experimental but it works for me.
use Template;
use strict;
use warnings;

my $t = Template->new;
my @nodes = (
{name => "one"},
{name => "two", children => [ {name => "red"},
                              {name => "green"} ] },
{name => "three", children => [
  {name => "blah", children => [
    {name => "yakko" },
    {name => "wacko" },
    {name => "dot" }
   ]
  }
]
} );


$t->process("rec.tt", { nodes => \@nodes } );

And the template:

[% VIEW nested_list %]
  [% BLOCK list %]
    <ul>
      [% FOREACH i IN item %]
        <li>[% i.name %]</li>
        [% IF i.children %]
          [% view.print(i.children.list) %]
        [% END %]
      [% END %]
    </ul>
  [% END %]
[% END %]

[% nested_list.print(nodes) %]

And that outputs:

  • one
  • two
    • red
    • green
  • three
    • blah
      • yakko
      • wacko
      • dot
Comment on Recursively display nested lists with Template Toolkit
Select or Download Code
Recursively display nested lists with Mason
by jeffa (Chancellor) on Aug 29, 2006 at 15:42 UTC

    Very nice. If you don't mind, i'd like to add how to do this with HTML::Mason for posterity, contrast/compare, etc.

    Calling component (nodes.html)

    <%init> use strict; use warnings; my @nodes = ( {name => "one"}, {name => "two", children => [ {name => "red"}, {name => "green"}, ] }, {name => "three", children => [ {name => "blah", children => [ {name => "yakko" }, {name => "wacko" }, {name => "dot" }, ] }, ] }, ); $m->comp( 'nodes.mas', nodes => \@nodes ); </%init>

    and nodes.mas

    <%args> @nodes </%args> <ul> % foreach my $i (@nodes) { <li><% $i->{name} %></li> % if (ref( $i->{children} ) eq 'ARRAY') { % $m->comp( 'nodes.mas', nodes => $i->{children} ); % } % } </ul>
    I added a check to ref() for safety.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Recursively display nested lists with Template Toolkit
by jasonk (Parson) on Sep 01, 2006 at 01:05 UTC

    Template Toolkit does allow templates to recurse, if you tell it to:

    RECURSION Flag to permit recursion into templates (default: 0). +

    We're not surrounded, we're in a target-rich environment!
      Thanks, jasonk. For some reason I missed that in the docs.
Re: Recursively display nested lists with Template Toolkit
by denny (Novice) on Jun 06, 2009 at 10:34 UTC
Re: Recursively display nested lists with Template Toolkit
by ferz (Initiate) on Mar 24, 2012 at 19:57 UTC
    I don't understand where print() method (or MACRO) is defined.

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-08-20 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (109 votes), past polls