> I made this FILTER for accessing "sprintf" from TT :
> $$self{FILTERS} ||= {
> sprintf => [ \&Page::Utils::sprintfTT, 1 ]
> };
>
> sub sprintfTT {
> my ($context, @args) = @_;
> return sub { sprintf $args[0], shift }
> };
>
> my question do I really need to make it dynamic.. I don't use the $context
> and I even don't want to use Text inside the tags (i more like to use it
> like VARIABLE VIRTUAL method )... I'm using it like this :
>
> <?
> IF field == 'Field' ;
> row.$field FILTER sprintf('%.4f') ;
> ELSE ; row.$field ;
> END
> ?>
Since it takes an argument the filter needs to be dynamic.
> I wondering why this doesn't work :
> <? field == 'Field' ? row.$field FILTER sprintf('%.4f') : row.$field ?>
The parser doesn't allow FILTER to be mixed inside expressions.
This will be fixed in v3.
> OR the best way should be :
> <? field == 'Field' ? row.$field.sprintf('%.4f') : row.$field ?>
> OR :
> <? field == 'Field' ? sprintf('%.4f', row.$field) : row.$field ?>
Instead of a filter you can add your own SCALAR_OPS to the Stash, eg:
use Template;
sub sprintfTT {
my ($str, $format, @extra) = @_;
return sprintf $format, $str, @extra;
};
$Template::Stash::SCALAR_OPS = {
'sprintf' => \&sprintfTT,
};
my $tt = Template->new();
$tt->process(\*DATA) || die $tt->error();
__DATA__
[% x = 3.141; x.sprintf("%10.5f"); %]
[% y = 1.618; y.sprintf("y = %.5f, x = %.5f", x); %]
Craig