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

Jay Hannah jhannah at mutationgrid.com
Mon Jan 10 17:53:22 PST 2011


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 at mutationgrid.com | http://mutationgrid.com | 1-402-598-7782







More information about the Omaha-pm mailing list