Perl Logic in Template Toolkit

460 views Asked by At

I have a currency value that I would like to format using Locale::Currency::Format however I have no idea how to format it.

Here is my output in Template Toolkit

[% amount %]

I would like this to be outputted using the following method:

currency_format('USD', amount, FMT_SYMBOL)

New to template toolkit so any help is appreciated.

3

There are 3 answers

0
RET On BEST ANSWER

I like Dave Cross' answer, and I agree with both he and codnodder about EVAL_PERL, which I've yet to find necessary as a solution in 7 or 8 years of almost daily TT use.

Personally, I would use:

[%- USE fmt = Class('Locale::Currency::Format'); -%]

<td>[% fmt.currency_format(var1, var2, var3) %]</td>

But if I was using this all the time, I'd be tempted to write a TT plugin wrapper around it.

0
codnodder On

If you have EVAL_PERL enabled in your "controller", you can use embedded perl to include the module and add a vmethod for example.

E.g.,

use strict;
use Template;

my $tt = Template->new(EVAL_PERL=>1);
my $out;
$tt->process(\*DATA, { amount => 50.34 }, \$out) or die $tt->error, "\n";
print $out;

__DATA__
[% PERL -%]
sub dollars { sprintf('$%0.02f', $_[0]); }
# or:
# use Local::Currency::Format;
# sub dollars { currency_format('USD', $_[0], FMT_SYMBOL); }
$stash->define_vmethod('scalar', 'dollars', \&dollars);
[% END -%]
The amount is [% amount.dollars %].

If you have some access to the "controller", you can add a FILTER.

use strict;
use Template;
#use Local::Currency::Format;  

my $tt = Template->new({
   #FILTERS => { 'dollars' => sub { currency_format('USD', $_[0], FMT_SYMBOL); } },
    FILTERS => { 'dollars' => sub { sprintf('$%0.02f', $_[0]); } },
});
my $out;
$tt->process(\*DATA, { amount => 50.34 }, \$out) or die $tt->error, "\n";
print $out;

__DATA__
The amount is [% amount | dollars %].

EDIT: Note that my use of sprintf to format the currency is just a placeholder. You would replace that with whatever module or method you choose.

3
Dave Cross On

I can't find Local::Currency::Format on CPAN, so I can't show you exactly how it works with this module. I can, however, show you the general direction you need to go in.

You have several options:

1/ Use currency_format to format the data before it is passed into the template.

my $amount = currency_format('USD', $amount, FMT_SYMBOL);
$tt->process($template_name, { amount => $amount, ... }) or die;

Then in the template you can just use [% amount %].

2/ Pass currency_format as a dynamic variable to the template.

$tt->process($template_name, {
  amount          => $amount,
  currency_format = > \&currency_format,
  ...
}) or die;

Then in the template, you can use currency_format as a function:

[% currency_format('USD', amount, FMT_SYMBOL) %]

3/ Write a real TT plugin for Local::Currency::Format.