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

Revision 796 (checked in by miyagawa, 18 years ago)

0.03

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