在使用 Template Toolkit 的 Catalyst 项目中显示 DBIx::Class ResultSet 的正确方法是什么?
给定一个 DBIx::Class 结果集,例如:
my $rs = $c->model("DB::Card")->search({family_name => "Smith"});
我读过的教程使用存储来传递行的 arrayref:
$c->stash->{cards} = [$rs->all];
这会导致此时执行查询,并将结果对象填充到存储中,以便它们可以在 TemplateToolkit 中用作:
[% FOREACH card IN cards %]
[% card.given_name %] [% card.family_name %]
[%END%]
是否有正确的方法让 TT 在从数据库中获取行时对其进行迭代?
Given a DBIx::Class resultset, for example:
my $rs = $c->model("DB::Card")->search({family_name => "Smith"});
the tutorials I've read use the stash to pass an arrayref of rows:
$c->stash->{cards} = [$rs->all];
This results in the query getting executed at this point, and the resulting objects stuffed into the stash, so they can be used in TemplateToolkit as:
[% FOREACH card IN cards %]
[% card.given_name %] [% card.family_name %]
[%END%]
Is there a proper way to have TT iterate over the rows as they get fetched from the DB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我正在做与作者完全相同的事情。
在尝试创建更严格的 MVC 方法时,我现在正在控制器中处理 DBIC 对象,并传递一个非常简单的存储以供模板显示。 (主要好处是代码可以被其他脚本重用,而不仅仅是 Web 界面。)
我很好奇是否有人知道这是否更有效,处理或内存方面。 我认为第一种方法会缩短处理时间,但保留内存的时间更长。 我猜我的新方法暂时需要更多的处理和更多的内存,但潜在的大结果集对象的寿命不会那么长。
I WAS doing exactly the same thing as the author.
In trying to create a more strict MVC approach, I'm now processing the DBIC objects in the controller and passing a very simple stash for the template to display. (Key benefit being the code is reusable by other scripts instead of just the web interface.)
I'm curious if anyone knows if this is more efficient or not, processing or memory-wise. I would think the first method results in less processing time but holds onto memory longer. I'd guess my new approach takes a bit more processing and a bit more memory temporarily, but the potentially large result set object doesn't live as long.
当然。 您可以将结果集直接传递给 TT 并在模板中对其进行迭代。
...进而:
Sure. You can pass the result set directly to TT and iterate over it in the template.
...and then:
或者,更好的是:
...在 TT 模板中:
Or, even better:
...in TT template:
我做:
在模板中:
I do:
In the template: