1#============================================================= -*-Perl-*- 2# 3# Template::Plugin::Format 4# 5# DESCRIPTION 6# 7# Simple Template Toolkit Plugin which creates formatting functions. 8# 9# AUTHOR 10# Andy Wardley <abw@wardley.org> 11# 12# COPYRIGHT 13# Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. 14# 15# This module is free software; you can redistribute it and/or 16# modify it under the same terms as Perl itself. 17# 18#============================================================================ 19 20package Template::Plugin::Format; 21 22use strict; 23use warnings; 24use base 'Template::Plugin'; 25 26our $VERSION = 2.70; 27 28 29sub new { 30 my ($class, $context, $format) = @_;; 31 return defined $format 32 ? make_formatter($format) 33 : \&make_formatter; 34} 35 36 37sub make_formatter { 38 my $format = shift; 39 $format = '%s' unless defined $format; 40 return sub { 41 my @args = @_; 42 push(@args, '') unless @args; 43 return sprintf($format, @args); 44 } 45} 46 47 481; 49 50__END__ 51 52=head1 NAME 53 54Template::Plugin::Format - Plugin to create formatting functions 55 56=head1 SYNOPSIS 57 58 [% USE format %] 59 [% commented = format('# %s') %] 60 [% commented('The cat sat on the mat') %] 61 62 [% USE bold = format('<b>%s</b>') %] 63 [% bold('Hello') %] 64 65=head1 DESCRIPTION 66 67The format plugin constructs sub-routines which format text according to 68a C<printf()>-like format string. 69 70=head1 AUTHOR 71 72Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/> 73 74=head1 COPYRIGHT 75 76Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. 77 78This module is free software; you can redistribute it and/or 79modify it under the same terms as Perl itself. 80 81=head1 SEE ALSO 82 83L<Template::Plugin> 84 85=cut 86 87# Local Variables: 88# mode: perl 89# perl-indent-level: 4 90# indent-tabs-mode: nil 91# End: 92# 93# vim: expandtab shiftwidth=4: 94