root/Template-Plugin-Comma/trunk/lib/Template/Plugin/Comma.pm

Revision 695 (checked in by miyagawa, 17 years ago)

0.02

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package Template::Plugin::Comma;
2
3 use strict;
4 use vars qw($VERSION);
5 $VERSION = 0.02;
6
7 require Template::Plugin::Filter;
8 use base qw(Template::Plugin::Filter);
9
10 use vars qw($DYNAMIC $FILTER_NAME);
11 $DYNAMIC = 1;
12 $FILTER_NAME = 'comma';
13
14 sub init {
15     my $self = shift;
16     my $name = $self->{_ARGS}->[0] || $FILTER_NAME;
17     $self->install_filter($name);
18     return $self;
19 }
20
21 sub filter {
22     my($self, $text, $args, $config) = @_;
23     return commify($text);
24 }
25
26 sub commify {
27     local $_  = shift;
28     1 while s/([-+]?\d+)(\d{3})/$1,$2/;
29     return $_;
30 }
31
32 1;
33 __END__
34
35 =head1 NAME
36
37 Template::Plugin::Comma - TT Plugin to commify numbers
38
39 =head1 SYNOPSIS
40
41   [% USE Comma %]
42
43   [% FILTER comma -%]
44   This item costs 10000 dollar.
45   [%- END %]
46
47   # Output:
48   # This item costs 10,000 dollar.
49
50   This item costs [% item.price | comma %] dollar.
51
52   # Output:
53   # This item costs 10,000 dollar.
54
55 =head1 DESCRIPTION
56
57 Template::Plugin::Comma is a plugin for TT, which allows you to
58 commify your numbers in templates. This would be especially useful for
59 prices.
60
61 =head1 NOTE
62
63 This module does nothing for I18N. If you want it, try
64 Template::Plugin::Number::Format.
65
66 =head1 AUTHOR
67
68 Original idea by Yoshiki Kurihara E<lt>kurihara@cpan.orgE<gt>
69
70 TT plugin implemented by Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
71
72 This library is free software; you can redistribute it and/or modify
73 it under the same terms as Perl itself.
74
75 =head1 SEE ALSO
76
77 L<Template>, C<Template::Plugin::Number::Format>
78
79 =cut
Note: See TracBrowser for help on using the browser.