blib/lib/HTML/Template/Filter/TT2.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran 60.0% 6 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 86.6% 39 / 45 CC 6 SCAR 15.7
CC
6
Cov
85%
__format_variable
6.7
BEGIN
1.0
BEGIN
1.0
CRAP 4.8
↑ Summary
1 package HTML::Template::Filter::TT2;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 require Exporter;
4  
5 {
6 3 ▶ no strict "vars";
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 $VERSION = '0.03';
8 3 @ISA = qw(Exporter);
9 3 @EXPORT = qw(ht_tt2_filter);
10 }
11  
12 =head1 NAME
13  
14 HTML::Template::Filter::TT2 - Template Toolkit 2 syntax for HTML::Template
15  
16 =head1 VERSION
17  
18 Version 0.03
19  
20 =head1 SYNOPSIS
21  
22 use HTML::Template::Filter::TT2;
23  
24 my $tmpl = HTML::Template->new(filter => \&ht_tt2_filter, ...);
25  
26 =head1 DESCRIPTION
27  
28 This C<HTML::Template> filter allows you to use a subset of the Template Toolkit 2
29 syntax, which is much less verbose than the default syntax. This is not
30 an emulation of TT2, so you're still limited to the usual C<HTML::Template>
31 semantics. Also, in order to keep the filter fast and simple, the
32 C<[% end %]> must be written with the block name. See below for details.
33  
34 =head1 SYNTAX
35  
36 Here is the syntax recognised by this module.
37  
38 =head2 Variables
39  
40 Simple interpolation:
41  
42 [% variable %]
43  
44 Interpolation with default value:
45  
46 [% variable :default %]
47  
48 Interpolation with filter (i.e. a C<HTML::Template> escape mode):
49  
50 [% variable |filter %]
51  
52 Interpolation with default value and filter:
53  
54 [% variable :default |filter %]
55  
56 =head2 If statements
57  
58 [% if condition %] ... [% else %] ... [end_if %]
59  
60 The difference with the actual TT2 syntax is that you must use C<end_if>
61 instead of C<end>
62  
63 =head2 Loops
64  
65 [% loop loop-name %] ... [% end_loop %]
66  
67 As for the C<if> statement, you must use C<end_loop> instead of C<end>.
68  
69 =head1 EXPORT
70  
71 Exports the C<ht_tt2_filter> function by default.
72  
73 =head1 FUNCTIONS
74  
75 =head2 ht_tt2_filter()
76  
77 Pass a reference to this function to the C<filter> parameter when calling
78 C<< HTML::Template->new() >>
79  
80 =cut
81  
82 8 ▶sub ht_tt2_filter {
Subroutine ht_tt2_filter: called CC=1 SCAR=0.0
Pod documented
83 8 my ($text_ref) = @_;
84 8 $$text_ref =~ s{\[% *(loop|if|unless) +(\w+) *%\]}{<TMPL_\U$1\E $2>}gm;
85 8 $$text_ref =~ s{\[% *(else) *%\]}{<TMPL_\U$1\E>}gm;
86 8 $$text_ref =~ s{\[% *end_(\w+) *%\]}{</TMPL_\U$1\E>}gm;
87 8 $$text_ref =~ s{
88 \[% # begin tag
89 \s* (\w+) \s* # variable name
90 \s* (?:: \s* (.+?))? # optional default value
91 \s* (?:\| \s* (\w+))? # optional filter
92 \s* %\] # end tag
93 }
94 7 {__format_variable($1, $2, $3)}gemx;
95 }
96  
97 7 ▶sub __format_variable {
Subroutine __format_variable: called CC=6 SCAR=19.1
98 7 my ($var, $default, $filter) = @_;
99  
100 # variable name
101 7 my $ht_syntax = "<TMPL_VAR NAME=$var";
102  
103 # handle default value
104 7 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 3 4 7
105 # autoquote unquoted values
106 3 ▶ if ($default !~ /^["']/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($default =~ /^["']/) 3 0 3
107 3 ▶ if ($default !~ /"/) { $default = qq/"$default"/ }
Branch: 2 branches
BranchTrueFalseTotal
if (not $default =~ /"/) { } 3 0 3
elsif (not $default =~ /'/) { } 0 0 0
108 0 elsif ($default !~ /'/) { $default = qq/'$default'/ }
109 0 else { warn "Can't handle unquoted value '$default' for variable $var" }
110 }
111  
112 3 $ht_syntax .= " DEFAULT=$default";
113 }
114  
115 # handle escape filter
116 7 ▶ $ht_syntax .= " ESCAPE=$filter" if defined $filter;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter 2 5 7
117  
118 # end tag
119 7 $ht_syntax .= ">";
120  
121 7 return $ht_syntax;
122 }
123  
124  
125 =head1 AUTHOR
126  
127 SE<eacute>bastien Aperghis-Tramoni, C<< <sebastien at aperghis.net> >>
128  
129 =head2 SEE ALSO
130  
131 L<HTML::Template>, L<Template::Toolkit>
132  
133 =head1 BUGS
134  
135 Please report any bugs or feature requests to
136 C<bug-html-template-filter-tt2 at rt.cpan.org>, or through the web interface at
137 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-Template-Filter-TT2>.
138 I will be notified, and then you'll automatically be notified of progress on
139 your bug as I make changes.
140  
141 =head1 SUPPORT
142  
143 You can find documentation for this module with the perldoc command.
144  
145 perldoc HTML::Template::Filter::TT2
146  
147 You can also look for information at:
148  
149 =over 4
150  
151 =item * AnnoCPAN: Annotated CPAN documentation
152  
153 L<http://annocpan.org/dist/HTML-Template-Filter-TT2>
154  
155 =item * CPAN Ratings
156  
157 L<http://cpanratings.perl.org/d/HTML-Template-Filter-TT2>
158  
159 =item * RT: CPAN's request tracker
160  
161 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Template-Filter-TT2>
162  
163 =item * Search CPAN
164  
165 L<http://search.cpan.org/dist/HTML-Template-Filter-TT2>
166  
167 =back
168  
169 =head1 COPYRIGHT & LICENSE
170  
171 Copyright 2007 SE<eacute>bastien Aperghis-Tramoni, all rights reserved.
172  
173 This program is free software; you can redistribute it and/or modify it
174 under the same terms as Perl itself.
175  
176 =cut
177  
178 3 1; # End of HTML::Template::Filter::TT2
↑ Summary