[Catalyst] Format last modified date field ...

Octavian Râsnita orasnita at gmail.com
Tue Dec 29 10:18:35 GMT 2009


From: "J. Shirley" <jshirley at gmail.com>

On Mon, Dec 28, 2009 at 1:17 PM, Kiffin Gish <kiffin.gish at planet.nl> wrote:
> I'm using a last_modified field which is being displayed like this
> "2009-12-28T18:25:28" (what's that 'T' doing in there?) but want to use
> a different format, how?
>
> In the list.tt2 file for listing users, I have:
>
> [% WHILE (user = users_rs.next) -%]
> [% FOREACH col IN users_rs.result_source.columns -%]
> [% user.$col %]</td>
> [% END %]
> [% END -%]
>
> In the schema Users:
>
> __PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp",
> "Core");
> __PACKAGE__->table("users");
> __PACKAGE__->add_columns(
> ...
> "last_modified",
> {
> data_type => "DATETIME",
> default_value => undef,
> is_nullable => 1,
> size => undef,
> },
> );
>
> Thanks alot in advance.
>
>
> --
> Kiffin Gish <Kiffin.Gish at planet.nl>
> Gouda, The Netherlands
>
>
>


This recipe is a bit premature to release, and I've been meaning to
for a while but I am happy with it and it works well enough for me.  I
had to rip out some other bits, so it may not compile right out.

I posted the gist here: http://gist.github.com/264994

The general idea is that in View::TT configuration, I define whatever
formats I want.  Then in TT, if I have a date I just use the [%
date_whatever | date_long %]

Anything in the "date" group creates a filter called "date_long" or
"date_whatever".

I have used this to create a lot of various template filters that are
sourced by config, though.

Feedback welcome, I'll build a blog entry and cross-post to the wiki
after I clean it up.
-J


I use a template which is included automaticly in each template, and I 
defined more macros in it, one that formats a DateTime column taking only 
the date, one that gets the time also, one that also prints the name of the 
week day, one that prints only the time, and other macros that formats a 
date string, not a DateTime object, and I can use any of those macros, 
depending on the result I want. (I also defined here the macro which is used 
for localizing the strings in all the templates.)

In order to include this template automaticly in each template, I use in the 
app config:

'View::TT' => {
PRE_PROCESS => ['locale_date_datetime.tt'],
},

Where locale_date_datetime.tt is the name of the template which is included 
below.

And it can be used like:

[% WHILE (row = table_name.next) -%]
[% d(row.date) %]
[% END -%]

Here is the content of this template:

[% #From a DateTime object: 23 feb. 2009 ~%]
[% MACRO d(date) BLOCK; date.set_locale(language).strftime('%e %b %Y');END 
~%]

[% #From a string with DateTime: 23 feb. 2009 ~%]
[% MACRO d2(date) BLOCK;
USE dt = DateTime(from_string => date, pattern => '%Y-%m-%d', time_zone => 
'Europe/Bucharest');
dt.set_locale(language).strftime('%e %b %Y');END ~%]

[% #From a DateTime object: 23 feb. 2009 14:30 ~%]
[% MACRO dt(date) BLOCK; date.set_locale(language).strftime('%e %b %Y 
%H:%M');END ~%]

[% #From a string with DateTime: 23 feb. 2009 14:30 ~%]
[% MACRO dt2(date) BLOCK;
USE dt = DateTime(from_string => date, pattern => '%Y-%m-%d %H:%M:%S', 
time_zone => 'Europe/Bucharest');
dt.set_locale(language).strftime('%A, %e %B %Y %H:%M');END ~%]

[% #From a DateTime object: marti, 23 februarie 2009 14:30 ~%]
[% MACRO dtd(date) BLOCK; date.set_locale(language).strftime('%A, %e %B %Y 
%H:%M');END ~%]

[% #From a string with DateTime: marti, 23 februarie 2009 14:30 ~%]
[% MACRO dtd2(date) BLOCK;
USE dt = DateTime(from_string => date, pattern => '%Y-%m-%d %H:%M:%S', 
time_zone => 'Europe/Bucharest');
dt.set_locale(language).strftime('%A, %e %B %Y %H:%M');END ~%]

[% #From a string with Date: marti, 23 februarie 2009 14:30 ~%]
[% MACRO dtd3(date) BLOCK;
USE dt = date;
dt.format(date, '%A, %d %B %Y %H:%M', lang);END ~%]

[% #From a DateTime object: 14:30 ~%]
[% MACRO t(date_time) BLOCK; 
date_time.set_locale(language).strftime('%H:%M');END ~%]

[% #translation ~%]
[% MACRO l(text,args) BLOCK; c.localize(text,args) || text; END ~%]

Octavian




More information about the Catalyst mailing list