Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 349
Thelema Avatar asked Jan 15 '09 15:01

Thelema


2 Answers

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 %]
like image 131
friedo Avatar answered Nov 15 '22 21:11

friedo


Or, even better:

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

...in TT template:

[% FOREACH card = cards %]
    [% card.given_name %] [% card.family_name %]
[% END %]
like image 45
delta Avatar answered Nov 15 '22 21:11

delta