Cybris has asked for the wisdom of the Perl Monks concerning the following question:

Oops, it seems I accidentally overwrote my original question, instead of replying to it. Sorry. Anyway, the question was how to determine the number of rows in a DBIx::Class::ResultSet object from within TT2.

I have found the solution myself:

[%# set the variable objects to an array of 0 or more objects, taken f +rom user.objects, which is a DBIx::Class::ResultSet %] [% IF user.objects %] [% IF user.objects.0 %] [% SET objects = user.objects %] [% ELSE %] [% SET objects = [ user.objects ] %] [% END %] [% ELSE %] [% SET objects = [] %] [% END %] Number of objects: [% objects.size %]

Unfortunately, it is not possible to write a macro for this, because TT macros can't return results...

Replies are listed 'Best First'.
Re: DBIx::Class and Template toolkit context problem
by LTjake (Prior) on Jan 05, 2010 at 15:42 UTC

    All relationships should have an _rs method for exactly this issue. Try:

    [% IF user.objects_rs.count %] ... [% END %]

    FYI, this is a FAQ. (See "How do I use DBIx::Class objects in my TT templates?")

    --
    "Go up to the next female stranger you see and tell her that her "body is a wonderland."
    My hypothesis is that she’ll be too busy laughing at you to even bother slapping you.
    " (src)

      Thank you very much, this is even better than my ugly workaround!