【发布时间】:2019-01-03 11:45:29
【问题描述】:
我使用模板合金和模板工具包,在 TT 中我想像使用 perl 一样检测数组引用:
for my $parents ( @{$value} ){
if (ref($parents) ne 'ARRAY'){
push @all_urls_names, $parents;
}
}
这是我在 tt 中的代码:
use warnings;
use v5.20; #strict is already set in the version
use Template;
my $t = Template->new(
INCLUDE_PATH => ['.'],
);
my @menus = (
["parent",
[qw(child1 child12 child13 child14) ]
],
);
my $mvs = {# my variables
menus => \@menus
};
$t->process("index.tt", $mvs, \my $out) || die $t->error;
sub {
return [ 200, [], [ $out ] ];
}
在 index.tt 中:
[% FOR base = menus %]
[% FOR parent = base %]
[% IF ref.parent ne "ARRAY" %]
<li>[% parent %] </li>
[% END %]
[% END %]
[% END %]
如果我删除 IF 语句,我会得到:
父母 数组(0x2539ad8)
我只想得到parent
我可以解决只是做 [% FOR parent = base.0 %]
但我想知道在 TT 中获取 ref 数组的解决方案。
【问题讨论】:
标签: perl template-toolkit