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:
[%
USE formatter = Class('DateTime::Format::Human::Duration');
MACRO format_duration_between(start, finish) BLOCK;
IF start AND finish;
formatter.new.format_duration_between(start.clone.truncate( to
=> 'second' ), finish.clone.truncate( to => 'second' ));
END;
END;
%]
I usually stick those in a separate file (like format.macro) and
include them where I need.
[% PROCESS format.macro %]
Time taken: [% format_duration_between(start_date, end_date) %]
If you need to do more complex perl based munging in your filter, then
you may need to write a custom plugin, or trow it in a class and use
it like I used the DateTime::Format::Human::Duration object in my
macro above.
Cheers,
Cees
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates