--- On Wed, 3/16/11, Cees Hek <[email protected]> wrote:
> From: Cees Hek <[email protected]> > Subject: Re: [Templates] object stringify in custom filter > To: "James.Q.L" <[email protected]> > Cc: [email protected] > Date: Wednesday, March 16, 2011, 7:17 PM > On Wed, Mar 16, 2011 at 4:13 PM, > James.Q.L <[email protected]> > wrote: > > Hi, > > > > in my TT, I pass a DateTime object(from DBIx::Class > and DBIx::Class::InflateColumn::DateTime). I can do > obj.year and all other DateTime operations in my template. > But the object gets stringified in my custom filter. I am > basically calling [% user.last_visited_date | time_ago %] > > > > and time_ago filter is something like > > > > sub timeago { > > sub { my $datetime_obj = shift; #do more work } > > } > > > > here I am expecting a DateTime obj in the custom > filter but instead it is stringify.. > > That is the way filters work. They filter the output > of a block, or > in this case an expression and it is always flattened to a > string. > You can solve this by using a macro instead. Here is > one that I use > for dates: [snip] nice to see you again, Cees - We went to the 2006 chicago yapc from to.pm. but I am now in Beijing. I have never touched macro before and I ought to learn more about TT. By the way. I got a suggestion from Stackoverflow to use custom plugin or vmethod and I like that way better. now in my CGI::Application app, I can do use DateTime::Duration::Fuzzy qw(time_ago); my $tt = __PACKAGE__->tt_obj; $tt->context->define_vmethod( 'hash', 'timeago', sub { time_ago( $_[0] ) } ); But It would be much cleaner if I can define the vmethod in TT's config along with custom filters in one place. James. _______________________________________________ templates mailing list [email protected] http://mail.template-toolkit.org/mailman/listinfo/templates
