在使用 Template Toolkit 的 Catalyst 项目中显示 DBIx::Class ResultSet 的正确方法是什么?

发布于 2024-07-12 12:50:26 字数 447 浏览 6 评论 0原文

给定一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千年*琉璃梦 2024-07-19 12:50:27

我正在做与作者完全相同的事情。

在尝试创建更严格的 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.

梦途 2024-07-19 12:50:26

当然。 您可以将结果集直接传递给 TT 并在模板中对其进行迭代。

$c->stash->{cards} = $rs;

...进而:

[% WHILE (card = cards.next) %]
    [% card.given_name %] [% card.family_name %]
[% END %]

Sure. You can pass the result set directly to TT and iterate over it in the template.

$c->stash->{cards} = $rs;

...and then:

[% WHILE (card = cards.next) %]
    [% card.given_name %] [% card.family_name %]
[% END %]
贵在坚持 2024-07-19 12:50:26

或者,更好的是:

$c->stash(cards => $rs);

...在 TT 模板中:

[% FOREACH card = cards %]
    [% card.given_name %] [% card.family_name %]
[% END %]

Or, even better:

$c->stash(cards => $rs);

...in TT template:

[% FOREACH card = cards %]
    [% card.given_name %] [% card.family_name %]
[% END %]
若相惜即相离 2024-07-19 12:50:26

我做:

@{$c->stash->{cards}} = $rs->all;

在模板中:

[% FOREACH card IN cards %]
    [% card.given_name %] [% card.family_name %]
[% END %]

I do:

@{$c->stash->{cards}} = $rs->all;

In the template:

[% FOREACH card IN cards %]
    [% card.given_name %] [% card.family_name %]
[% END %]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文