What's the right way to display a DBIx::Class ResultSet in my Catalyst project that uses Template Toolkit?
Asked Answered
D

4

10

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?

Downwash answered 15/1, 2009 at 15:59 Comment(0)
G
19

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 %]
Gatehouse answered 15/1, 2009 at 16:17 Comment(0)
B
5

Or, even better:

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

...in TT template:

[% FOREACH card = cards %]
    [% card.given_name %] [% card.family_name %]
[% END %]
Brainchild answered 23/6, 2009 at 16:35 Comment(2)
Does this work? - FOREACH takes an array, not a recordset, no?Downwash
I was struggling a lot but was using [% FOREACH foo IN foos %] but I see that [% FOREACH foo = foos %] works just fine ;)Undoing
N
2

I do:

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

In the template:

[% FOREACH card IN cards %]
    [% card.given_name %] [% card.family_name %]
[% END %]
Nordrheinwestfalen answered 12/8, 2009 at 22:23 Comment(1)
memory hungry with a large resultset.Childress
E
2

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.

Eared answered 30/3, 2010 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.