给定DBIx :: Class结果集,例如:
my $rs = $c->model("DB::Card")->search({family_name => "Smith"});
我读过的教程使用stash传递一行arrayref:
$c->stash->{cards} = [$rs->all];
这导致查询在此时执行,并且生成的对象填充到存储中,因此它们可以在TemplateToolkit中用作:
[% FOREACH card IN cards %] [% card.given_name %] [% card.family_name %] [%END%]
有没有一种正确的方法让TT在从数据库中获取时对行进行迭代?
当然.您可以将结果集直接传递给TT并在模板中迭代它.
$c->stash->{cards} = $rs;
...然后:
[% WHILE (card = cards.next) %] [% card.given_name %] [% card.family_name %] [% END %]
或者,甚至更好:
$c->stash(cards => $rs);
...在TT模板中:
[% FOREACH card = cards %] [% card.given_name %] [% card.family_name %] [% END %]