[Catalyst] iterating ResultSet in controller vs. template
Michael Reece
mreece at vinq.com
Tue Jun 19 20:37:12 GMT 2007
either use iterators in both controller and template, or use array-
refs in both, but don't try to use both at once with only one variable.
On Jun 19, 2007, at 12:09 PM, John Goulah wrote:
> On 6/19/07, Michael Reece <mreece at vinq.com> wrote:
> i believe you are getting an array-ref because you are calling it
> in list context via the [ ... ] here.
>
> my $myalums = [ $c->model('MyDB::Alumni')->search({}, { rows =>
> 20 }) ] ;
>
>
> try:
>
> my $myalums_iter = $c->model('MyDB::Alumni')->search({},
> { rows => 20 });
> while (my $alum = $myalums_iter->next) { ... }
>
>
>
> You are correct (and I did explain that as well in my original post
> that was happening). So the question is , how can I make the same
> call and access the data in both places?
pick one:
my $myalums = $c->stash->{alumni} = [ $c->model('MyDB::Alumni')-
>search({}, { rows => 20 }) ] ;
foreach my $alum (@$myalums) {
print "id: ", $alum->alum_id, "\n";
}
...
[% FOREACH alum IN alumni -%]
id: [% alum.alumni_id %] <br />
[% END -%]
-or-
my $myalums = $c->stash->{alumni} = $c->model('MyDB::Alumni')-
>search({}, { rows => 20 });
while (my $alum = $myalums->next) {
print "id: ", $alum->alum_id, "\n";
}
$myalums->reset; # reset iterator!
...
[%WHILE (alum = alumni.next) %]
id: [% alum.alumni_id %] <br />
[% END -%]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070619/5267e8f7/attachment-0001.htm
More information about the Catalyst
mailing list