Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get elements out of an array with Template Toolkit?

I have an array of Paths which i want to read out with Template Toolkit. How can I access the array Elements of this array? The Situation is this:

my @dirs;
opendir(DIR,'./directory/') || die $!;
@dirs = readdir(DIR);
close DIR;
$vars->{'Tree'} = @dirs;

Then I call the Template Page like this:

$template->process('create.tmpl', $vars) 
   || die "Template process failed: ", $template->error(), "\n";

In this template I want to make an Tree of the directories in the array. How can I access them?

My idea was to start with a foreach in the template like this

[% FOREACH dir IN Tree.dirs %]
$dir
[% END %]
like image 760
Przemek Avatar asked Mar 17 '10 14:03

Przemek


1 Answers

Use references to pass arrays or hashes into your template:

$vars->{'Tree'} = \@dirs;

Then in the template:

[% FOR d = Tree %]
    [% d %]
[% END %]
like image 135
Eugene Yarmash Avatar answered Sep 27 '22 20:09

Eugene Yarmash