[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Catalyst + Template Toolkit - custom filters



I didn't know this was quite this easy.

In Controller/View/TT.pm you can add arbitrary filters:

   __PACKAGE__->config(
      FILTERS => {
         commify  => \&commify,

Define your sub:

   # Perl Cookbook recipe 2.17
   sub commify {
      return 'N/A' unless defined $_[0] and length $_[0];
      my $text = reverse $_[0];
      $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
      return scalar reverse $text;
   }

And then your templates can use the new filter:

   [% num_views | commify %]  

"1234" becomes "1,234". Etc. Neat, huh?

Jay Hannah
Software Architect
jhannah@mutationgrid.com | http://mutationgrid.com | 1-402-598-7782