Reputation: 321
I try to display an array item in dancer, here is the code:
get '/' => sub {
my @rows = ('aaa','bbb','ccc');
template 'crud.tt', {'rows' => \@rows};
};
and the template is:
<h2><% $rows[1] %></h2>
<h2><% rows[1] %></h2>
<% FOREACH r IN rows %>
<p><% r %></p>
<% END %>
In the h2 element show nothing, what is the right way?
Upvotes: 2
Views: 964
Reputation: 126722
You can't pass anything but a simple scalar value if you are using the default Dancer
template engine. But if you enable Template::Toolkit
as the engine then all kinds of things are possible.
You can do this globally by setting template: template_toolkit
in the YAML config file, or you can set it just for this route by writing
get '/' => sub {
my @rows = ('aaa','bbb','ccc');
set template => 'template_toolkit';
template 'crud.tt', { rows => \@rows };
};
Your template will look like
<h2><% rows.1 %></h2>
<% FOREACH r IN rows %>
<p><% r %></p>
<% END %>
and you will need
use Template;
to load the Template::Toolkit
module before you use either method
Upvotes: 7