[英]Perl (Dancer) & Template Toolkit - looping thru hashref
I'm using Perl with Dancer and Template Toolkit . 我将Perl与Dancer和Template Toolkit一起使用 。
I'm passing a hashref to my template. 我正在将hashref传递给模板。
This is the way it's built, out of an array ( @musicList
): 这是通过数组(
@musicList
)构建的方式:
my $hashrMusic = {};
my $intCount = 0;
foreach my $track ( @musicList ) {
$hashrMusic->{ $intCount } = $track;
$intCount++ ;
}
This is the Dumper
version of the hash: 这是哈希的
Dumper
版本:
$VAR1 = {
'1049' => '09 Faruk\'s Funk (Matt Stein + Nickodemus Rework).mp3',
'127' => '45 There She Goes.mp3',
'71' => 'Kenny Wayne Shepherd - One Foot On The Pass.mp3'
};
This is the way I pass the hashref to the template: 这是我将hashref传递给模板的方式:
template 'scan.tt', {
'countTracks' => scalar keys %$hashrMusic,
'tracks' => $hashrMusic,
'dump' => Dumper($hashrMusic),
}
Now I'm trying to loop through the hasref to display it, using the following TT code: 现在,我尝试使用以下TT代码遍历hasref来显示它:
<ul>
<% FOREACH track IN tracks %>
<li><% track.value %></li>
<% END %>
</ul>
This produces no output (but countTracks
is OK, just as dump
). 这不会产生任何输出(但是
countTracks
可以,就像dump
)。 Any hint/idea? 有任何提示/想法吗?
You forgot the closing /li. 您忘记了/ li。 Also you're misusing FOREACH with respect to hashrefs.
另外,您在滥用hashrefs的FOREACH。
Try this: 尝试这个:
<ul>
<% FOREACH track IN tracks %>
<li>
The key is: <% track.key %>
The filename is: <% track.value.filename %>
</li>
<% END %>
</ul>
For complete documentation on FOREACH, go here: 有关FOREACH的完整文档,请转到此处:
http://www.template-toolkit.org/docs/manual/Directives.html#section_FOREACH http://www.template-toolkit.org/docs/manual/Directives.html#section_FOREACH
Dancer is not using TemplateToolkit by default, but a look-like template engine that does not support TT tags. Dancer默认情况下不使用TemplateToolkit,而是不支持TT标签的外观类似的模板引擎。 Digging in the config files and configuring the template engine to be TT solved the issue.
挖掘配置文件并将模板引擎配置为TT解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.