[Templates] process lists of array_refs with Template-Toolkit?
Axel Gerstmair
Axel Gerstmair <anthill@web.de>
Mon, 20 Oct 2003 13:24:28 +0200
On Monday, October 20, 2003 at 11:56, mt wrote:
> i want to know if it is in general possible to process lists of
> array_refs with Template-Toolkit.
> [...]
> is it possible to process arrays of array, hashes of arrays, lists of
> array_refs with Template?
Sure, Template Toolkit supports all these standard Perl constructs. I
didn't really understand your code snippets :-), but you can do the
following things (and more):
in Perl:
my $vars = {
'myarrayref' => [1, 2, 3],
'myhashref' => {
1 => 'one',
2 => 'two',
3 => 'three',
},
'mynestedstruct' => {
nestedlist => ['x', 'y', 'z'],
},
'mynestedlist' => [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
],
};
# ...
$template->process('test.tt2', $vars);
in test.tt2:
List all entries of a simple list:
[% FOREACH item = myarrayref %]
[% item %]
[% END %]
List all entries of a simple hashtable:
[% FOREACH key = myhashref.keys %]
[% key %] => [% myhashref.$key %]
[% END %]
Show first list entry of hash element 'nestedlist':
[% mynestedstruct.nestedlist.0 %]
List all entries of a doubly nested list:
[% FOREACH outer = mynestedlist %]
[% FOREACH inner = outer %]
[% inner %]
[% END %]
[% END %]
I hope you get the drift. The TT manual provides many helpful hints,
too.
Best regards,
Axel