Perl dancer:模板中的foreach仅打印第一个值
我在 Dancer 中遇到了一个非常简单的问题:我有一系列名称,我想在模板中打印每个名称。这些名称来自外部来源(不是数据库)。但是,当我尝试对模板中的列表进行 foreach 时,我只得到第一个值。
代码:
use Dancer;
use Template;
set 'template' => 'template_toolkit';
get '/' => sub {
my @list = ("one","two","three");
template 'list.tt', {
'values' => @list,
};
};
dance;
和模板:
<ul>
<%FOREACH item IN values %>
<li><% item %></li>
<%END%>
</ul>
这只输出一个包含单个项目“one”的列表。我缺少什么?
I have what should be a really simple problem in Dancer: I have an array of names, and I'd like to print each one in a template. These names come from an outside source (not a database). However, when I try to do a foreach over the list in the template, I only get the first value.
Code:
use Dancer;
use Template;
set 'template' => 'template_toolkit';
get '/' => sub {
my @list = ("one","two","three");
template 'list.tt', {
'values' => @list,
};
};
dance;
And template:
<ul>
<%FOREACH item IN values %>
<li><% item %></li>
<%END%>
</ul>
This only outputs a list with a single item, "one". What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表达式
'values' =>; @list
扩展为包含"values" "one" "two" "third"
的列表,因此您应该尝试使用对数组的引用:上面仍然复制
@list
并返回一个引用。如果您想获取对已存在数组的引用,请使用\@list
。The expression
'values' => @list
expands to a list that contains"values" "one" "two" "three"
, so you should try with a reference to the array instead:The above still copies
@list
and returns a reference. If you want to fetch a reference to the already existing array, use\@list
.我敢打赌这是因为您必须传递对
'values'
的数组引用:否则列表会扩展并且您实际上正在传递:
I'll wager it's because you have to pass an array reference to the
'values'
:Otherwise the list gets expanded and you're actually passing: