lib/Templer/Plugin/Dollar.pm

filter: Statementstmt 100% 16 / 16 Branchbran n/a Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 3 / 3 totaltotal 86.2% 25 / 29 CC 2 SCAR 3.0
CC
2
Cov
89%
new
2.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.4
« lib/Templer/Plugin/HTML.pm ↑ Summary lib/Templer/Plugin/Strict.pm »
1  
2 =head1 NAME
3  
4 Templer::Plugin::Dollar - A simple shell-like syntax for template variables.
5  
6 =cut
7  
8 =head1 DESCRIPTION
9  
10 This class implements a layout template filter plugin for C<templer> which
11 allows template variables to be included using a dollar sign followed by an
12 open brace, the variable name and a closing brace.
13  
14 This allows template such as this to be used
15  
16 =for example begin
17  
18 <html>
19 <head>
20 <title>${title escape="html"}</title>
21 <meta name='author' content='${author} ${email escape=url}'/>
22 </head>
23 <body>
24 </body>
25 </html>
26  
27 =for example end
28  
29 Everything between the variable name and the closing brace is used B<as is> in
30 the transformation. The third line of the example is for instance transformed
31 as
32  
33 =for example begin
34  
35 <title><tmpl_var name="title" escape="html"></title>
36  
37 =for example end
38  
39 =cut
40  
41 =head1 LICENSE
42  
43 This module is free software; you can redistribute it and/or modify it
44 under the terms of either:
45  
46 a) the GNU General Public License as published by the Free Software
47 Foundation; either version 2, or (at your option) any later version,
48 or
49  
50 b) the Perl "Artistic License".
51  
52 =cut
53  
54 =head1 AUTHOR
55  
56 Bruno Beaufils <bruno@boulgour.com>
57  
58 =cut
59  
60 =head1 COPYRIGHT AND LICENSE
61  
62 Copyright (C) 2015 Bruno Beaufils <bruno@boulgour.com>.
63  
64 This library is free software. You can modify and or distribute it under
65 the same terms as Perl itself.
66  
67 =cut
68  
69 =head1 METHODS
70  
71 =cut
72  
73  
74 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77  
78 package Templer::Plugin::Dollar;
79  
80  
81 =head2 new
82  
83 Constructor. No arguments are supported/expected.
84  
85 =cut
86  
87 sub new
88 6 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
89 6 my ( $proto, %supplied ) = (@_);
90 6 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
91  
92 6 my $self = {};
93 6 bless( $self, $class );
94 6 return $self;
95 }
96  
97  
98 =head2 available
99  
100 This plugin is always available.
101  
102 =cut
103  
104 sub available
105 1 ▶{
Subroutine available: called CC=1 SCAR=0.0
Pod documented
106 1 return 1;
107 }
108  
109  
110 =head2 filter
111  
112 Filter the given template.
113  
114 =cut
115  
116 sub filter
117 3 ▶{
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
118 3 my ( $self, $str ) = (@_);
119  
120 3 $str =~ s/\$\{([^:\s}]+)([^}]*)\}/<tmpl_var name="$1"$2>/g;
121  
122 3 return $str;
123 }
124  
125 11 Templer::Plugin::Factory->new()
126 ->register_filter( "dollar", "Templer::Plugin::Dollar" );
« lib/Templer/Plugin/HTML.pm ↑ Summary lib/Templer/Plugin/Strict.pm »