I've been using something along the lines of this (with corresponding CSS) fairly successfully.

use strict; use Template (); use Scalar::Util qw( looks_like_number ); sub fmt_currency { my( $txt ) = @_; return $txt unless looks_like_number( $txt ); if( $txt < 0 ) { $txt = '<span class="neg">(' . abs( $txt ) . ')</span>' } else { ## maybe wrap $txt with a pos span . . . } return $txt; } my $t = Template->new( FILTERS => { fmt_currency => \&fmt_currency }, ## ... ); my $data = { qw/foo 1.23 bar -3.45 baz quux/ }; my %tmpl_src = <<EOT; [% foo | fmt_currency %] [% bar | fmt_currency %] [% baz | fmt_currenc +y %] EOT $t->process( \$tmpl_src, $data, \*STDOUT ); exit 0; __END__ 1.23 <span class="neg">(3.45)</span> quux

Update: If you need to get fancier (e.g. passing a sprintf-style format string) see the Template::Plugin::Filter documentation for how to write and specify dynamic filters. Also the badger book has a relevant section as well.

Update 2: OK, I was bored and wanted to see if I remembered how to do it. Dynamic version follows for those who're really interested.

#!/opt/local/bin/perl use strict; use Template (); use Scalar::Util qw( looks_like_number ); sub fmt_currency_factory { my ($ctx, $fmt) = @_; $fmt = "%0.4f" unless defined $fmt; return sub { my ($txt) = @_; return $txt unless looks_like_number($txt); my $val = sprintf( $fmt, abs($txt) ); if ( $txt < 0 ) { $txt = '<span class="neg">(' . $val . ')</span>'; } else { $txt = $val; } return $txt; } } my $t = Template->new( FILTERS => { fmt_currency => [ \&fmt_currency_factory, 1 ] }, ## ... ); my $data = { qw/foo 1.23 bar -3.45 baz quux/ }; my $tmpl_src = <<EOT; [% foo | fmt_currency %] [% bar | fmt_currency( "%0.1f" ) %] [% baz | fmt_currency %] EOT $t->process( \$tmpl_src, $data, \*STDOUT ) or die $t->error(); __END__ 1.2300 <span class="neg">(3.5)</span> quux

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Template::Toolkit and Currency Formatting by Fletch
in thread Template::Toolkit and Currency Formatting by greg_coates

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.