Tenjin/Context.pm

filter: Statementstmt 84.7% 61 / 72 Branchbran 33.3% 6 / 18 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 70.3% 83 / 118 SCAR 29.5
CC
14
Cov
70%
to_func
22.9
evaluate
6.4
_build_decl
3.0
CRAP 19.1
« Tenjin.pm ↑ Summary Tenjin/Preprocessor.pm »
1 package Tenjin::Context;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Tenjin::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 our $VERSION = "1.000000";
9 8 $VERSION = eval $VERSION;
10  
11 =head1 NAME
12  
13 Tenjin::Context - In charge of managing variables passed to Tenjin templates.
14  
15 =head1 SYNOPSIS
16  
17 # this module is used internally, but if you insist, it is
18 # in charge of the context object:
19  
20 # in your templates (unnecessary, for illustration purposes):
21 <title>[== $_context->{title} =]</title>
22 # instead use:
23 <title>[== $title =]</title>
24  
25 =head1 DESCRIPTION
26  
27 This module is in charge of managing Perl variables that are passed to
28 templates upon rendering for direct usage. The context object is simply
29 a hash-ref of key-value pairs, which are made available for templates
30 as "standalone variables" named for each key in the hash-ref.
31  
32 This module is also in charge of the actual rendering of the templates,
33 or more correctly, for evaluating the Perl code created from the templates,
34 first integrating the context variables to them, and returning the rendered
35 output.
36  
37 Finally, this module makes the Tenjin utility methods of L<Tenjin::Util>
38 available natively inside templates. See L<Tenjin::Util> for more info.
39  
40 =head1 INTERNAL METHODS
41  
42 =head2 new( [\%vars] )
43  
44 Constructs a new context object, which is basically a hash-ref of key-value pairs
45 which are passed to templates as variables. If a C<$vars> hash-ref is passed
46 to the constructor, it will be augmented into the created object.
47  
48 To illustrate the context object, suppose it looks like so:
49  
50 {
51 scalar => 'I am a scalar',
52 arrayref => [qw/I am an array/],
53 hashref => { i => 'am', a => 'hash-ref' },
54 }
55  
56 Then the variables C<$scalar>, C<$arrayref> and C<$hashref> will be available for
57 direct usage inside your templates, and you can dereference the variables
58 normally (i.e. C<@$arrayref> and C<%$hashref>).
59  
60 =cut
61  
62 sub new {
63 13 ▶ my ($class, $self) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
64  
65 13 ▶ $self ||= {};
Condition50%1/2
$self ||= {}
l!l
130
MC/DC0%0/1
$self ||= {}
$self
Truth table50%1/2
A
A= $self
Aresult
11
00
66  
67 13 return bless $self, $class;
68 }
69  
70 =head2 evaluate( $script, $template_name )
71  
72 This method receives a compiled template and actually performes the evaluation
73 the renders it, then returning the rendered output. If Tenjin is configured
74 to C<use strict>, the script will be C<eval>ed under C<use strict>.
75  
76 =cut
77  
78 sub evaluate {
79 23 ▶ my ($self, $script, $name) = @_;
Subroutine evaluate: called CC=6 SCAR=18.5
Pod documented
80  
81 23 my $_context = $self;
82 23 ▶ $script = ($script =~ /\A(.*)\Z/s) && $1 if $Tenjin::BYPASS_TAINT;
Branch: 1 branch
BranchTrueFalseTotal
if $Tenjin::BYPASS_TAINT 23 0 23
Condition33%1/3
$script =~ /\A(.*)\Z/s && $1
!ll&&!rl&&r
0023
MC/DC0%0/2
$script =~ /\A(.*)\Z/s && $1
$script =~ /\A(.*)\Z/s $1
Truth table33%1/3
A && B
A= $script =~ /\A(.*)\Z/sB= $1
ABresult
0X0
100
111
83 23 ▶ my $s = $name ? "# line 1 \"$name\"\n" : ''; # line directive
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 23 0 23
84 23 $s .= $script;
85  
86 23 my $ret;
87 23 ▶ if ($Tenjin::USE_STRICT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Tenjin::USE_STRICT) { } 0 23 23
88 0 $ret = eval($s);
89 } else {
90 8 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 23 $ret = eval($s);
92 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 }
94  
95 23 ▶ croak "[Tenjin] Failed rendering $name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 22 23
96
97 22 return $ret;
98 }
99  
100 =head2 to_func( $script, [$filename] )
101  
102 This method receives the script created when reading a template and wraps
103 it in a subroutine, C<eval>s it and returns the rendered output. This method
104 is called when compiling the template.
105  
106 =cut
107  
108 sub to_func {
109 0 ▶ my ($self, $script, $name) = @_;
Subroutine to_func: not called CC=6 SCAR=31.3
Pod documented
110  
111 0 ▶ $script = ($script =~ /\A(.*)\Z/s) && $1 if $Tenjin::BYPASS_TAINT;
Branch: 1 branch
BranchTrueFalseTotal
if $Tenjin::BYPASS_TAINT 0 0 0
Condition0%0/3
$script =~ /\A(.*)\Z/s && $1
!ll&&!rl&&r
000
MC/DC0%0/2
$script =~ /\A(.*)\Z/s && $1
$script =~ /\A(.*)\Z/s $1
Truth table0%0/3
A && B
A= $script =~ /\A(.*)\Z/sB= $1
ABresult
0X0
100
111
112 0 ▶ my $s = $name ? "# line 1 \"$name\"\n" : ''; # line directive
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
113 0 $s .= "sub { my (\$_context) = \@_; $script }";
114
115 0 my $ret;
116 0 ▶ if ($Tenjin::USE_STRICT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Tenjin::USE_STRICT) { } 0 0 0
117 0 $ret = eval($s);
118 } else {
119 8 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 0 $ret = eval($s);
121 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 }
123  
124 0 ▶ croak "[Tenjin] Failed compiling $name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
125
126 0 return $ret;
127 }
128  
129 =head2 _build_decl()
130  
131 This method is in charge of making all the key-value pairs of the context
132 object available to templates directly by the key names. This is simply done
133 by traversing the key-value pairs of the context object and adding an
134 assignment line between a scalar variable named as the key and its appropriate
135 value.
136  
137 =cut
138  
139 sub _build_decl {
140 23 ▶ my $self = shift;
Subroutine _build_decl: called CC=3 SCAR=11.0
141  
142 23 my $s = '';
143 23 foreach my $k (keys %$self) {
144 58 ▶ next if $k eq '_context';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "_context" 0 58 58
145 58 $s .= "my \$$k = \$_context->{'$k'}; ";
146 }
147 23 return $s;
148 }
149  
150 =head1 UTILITY METHODS
151  
152 These methods are defined in L<Tenjin::Util> and used here so they are
153 made available natively inside templates. See L<Tenjin::Util> for more
154 information.
155  
156 =head2 _p( $expr )
157  
158 =head2 _P( $expr )
159  
160 =head2 escape( $expr )
161  
162 =head2 escape_xml( $expr )
163  
164 =head2 unescape_xml( $expr )
165  
166 =head2 encode_url( $url )
167  
168 =head2 decode_url( $url )
169  
170 =head2 checked( $expr )
171  
172 =head2 selected( $expr )
173  
174 =head2 disabled( $expr )
175  
176 =head2 nl2br( $text )
177  
178 =head2 text2html( $text )
179  
180 =head2 tagattr( $name, $expr, [$value] )
181  
182 =head2 tagattrs( %attrs )
183  
184 =head2 new_cycle( @items )
185  
186 =cut
187  
188 # this makes the Tenjin utility methods available to templates 'natively'
189 8 *_p = *Tenjin::Util::_p;
190 8 *_P = *Tenjin::Util::_P;
191 8 *escape = *Tenjin::Util::escape_xml;
192 8 *escape_xml = *Tenjin::Util::escape_xml;
193 8 *unescape_xml = *Tenjin::Util::unescape_xml;
194 8 *encode_url = *Tenjin::Util::encode_url;
195 8 *decode_url = *Tenjin::Util::decode_url;
196 8 *checked = *Tenjin::Util::checked;
197 8 *selected = *Tenjin::Util::selected;
198 8 *disabled = *Tenjin::Util::disabled;
199 8 *nl2br = *Tenjin::Util::nl2br;
200 8 *text2html = *Tenjin::Util::text2html;
201 8 *tagattr = *Tenjin::Util::tagattr;
202 8 *tagattrs = *Tenjin::Util::tagattrs;
203 8 *new_cycle = *Tenjin::Util::new_cycle;
204  
205 8 1;
206  
207 =head1 SEE ALSO
208  
209 L<Tenjin>, L<Tenjin::Util>, L<Tenjin::Template>.
210  
211 =head1 AUTHOR, LICENSE AND COPYRIGHT
212  
213 See L<Tenjin>.
214  
215 =cut
« Tenjin.pm ↑ Summary Tenjin/Preprocessor.pm »