File Coverage

blib/lib/Tenjin/Context.pm
Criterion Covered Total %
statement 43 54 79.6
branch 6 18 33.3
condition 2 8 25.0
subroutine 11 12 91.6
pod 3 3 100.0
total 65 95 68.4


line stmt bran cond sub pod time code
1             package Tenjin::Context;
2              
3 7     7   103 use strict;
  7         14  
  7         227  
4 7     7   38 use warnings;
  7         12  
  7         177  
5 7     7   3755 use Tenjin::Util;
  7         18  
  7         263  
6 7     7   69 use Carp;
  7         11  
  7         2085  
7              
8             our $VERSION = "0.070001";
9             $VERSION = eval $VERSION;
10              
11             =head1 NAME
12              
13             Tenjin::Context - In charge of managing variables passed to Tenjin templates.
14              
15             =head1 VERSION
16              
17             version 0.070001
18              
19             =head1 SYNOPSIS
20              
21             # this module is used internally, but if you insist, it is
22             # in charge of the context object:
23              
24             # in your templates (unnecessary, for illustration purposes):
25             [== $_context->{title} =]
26             # instead use:
27             [== $title =]
28              
29             =head1 DESCRIPTION
30              
31             This module is in charge of managing Perl variables that are passed to
32             templates upon rendering for direct usage. The context object is simply
33             a hash-ref of key-value pairs, which are made available for templates
34             as "standalone variables" named for each key in the hash-ref.
35              
36             This module is also in charge of the actual rendering of the templates,
37             or more correctly, for evaluating the Perl code created from the templates,
38             first integrating the context variables to them, and returning the rendered
39             output.
40              
41             Finally, this module makes the Tenjin utility methods of L
42             available natively inside templates. See L for more info.
43              
44             =head1 INTERNAL METHODS
45              
46             =head2 new( [\%vars] )
47              
48             Constructs a new context object, which is basically a hash-ref of key-value pairs
49             which are passed to templates as variables. If a C<$vars> hash-ref is passed
50             to the constructor, it will be augmented into the created object.
51              
52             To illustrate the context object, suppose it looks like so:
53              
54             {
55             scalar => 'I am a scalar',
56             arrayref => [qw/I am an array/],
57             hashref => { i => 'am', a => 'hash-ref' },
58             }
59              
60             Then the variables C<$scalar>, C<$arrayref> and C<$hashref> will be available for
61             direct usage inside your templates, and you can dereference the variables
62             normally (i.e. C<@$arrayref> and C<%$hashref>).
63              
64             =cut
65              
66             sub new {
67 11     11 1 29 my ($class, $self) = @_;
68              
69 11   50     45 $self ||= {};
70              
71 11         53 return bless $self, $class;
72             }
73              
74             =head2 evaluate( $script, $template_name )
75              
76             This method receives a compiled template and actually performes the evaluation
77             the renders it, then returning the rendered output. If Tenjin is configured
78             to C, the script will be Ced under C.
79              
80             =cut
81              
82             sub evaluate {
83 21     21 1 77 my ($self, $script, $name) = @_;
84              
85 21         39 my $_context = $self;
86 21 50 33     350 $script = ($script =~ /\A.*\Z/s) && $& if $Tenjin::BYPASS_TAINT;
87 21 50       104 my $s = $name ? "# line 1 \"$name\"\n" : ''; # line directive
88 21         58 $s .= $script;
89              
90 21         34 my $ret;
91 21 50       63 if ($Tenjin::USE_STRICT) {
92 0         0 $ret = eval($s);
93             } else {
94 7     7   48 no strict;
  7         19  
  7         371  
95 21         9522 $ret = eval($s);
96 7     7   38 use strict;
  7         20  
  7         1385  
97             }
98              
99 21 100       740 croak "[Tenjin] Failed rendering $name: $@" if $@;
100            
101 20         116 return $ret;
102             }
103              
104             =head2 to_func( $script, [$filename] )
105              
106             This method receives the script created when reading a template and wraps
107             it in a subroutine, Cs it and returns the rendered output. This method
108             is called when compiling the template.
109              
110             =cut
111              
112             sub to_func {
113 0     0 1 0 my ($self, $script, $name) = @_;
114              
115 0 0 0     0 $script = ($script =~ /\A.*\Z/s) && $& if $Tenjin::BYPASS_TAINT;
116 0 0       0 my $s = $name ? "# line 1 \"$name\"\n" : ''; # line directive
117 0         0 $s .= "sub { my (\$_context) = \@_; $script }";
118            
119 0         0 my $ret;
120 0 0       0 if ($Tenjin::USE_STRICT) {
121 0         0 $ret = eval($s);
122             } else {
123 7     7   70 no strict;
  7         13  
  7         243  
124 0         0 $ret = eval($s);
125 7     7   34 use strict;
  7         15  
  7         2390  
126             }
127              
128 0 0       0 croak "[Tenjin] Failed compiling $name: $@" if $@;
129            
130 0         0 return $ret;
131             }
132              
133             =head2 _build_decl()
134              
135             This method is in charge of making all the key-value pairs of the context
136             object available to templates directly by the key names. This is simply done
137             by traversing the key-value pairs of the context object and adding an
138             assignment line between a scalar variable named as the key and its appropriate
139             value.
140              
141             =cut
142              
143             sub _build_decl {
144 21     21   44 my $self = shift;
145              
146 21         44 my $s = '';
147 21         123 foreach my $k (keys %$self) {
148 54 50       136 next if $k eq '_context';
149 54         214 $s .= "my \$$k = \$_context->{'$k'}; ";
150             }
151 21         159 return $s;
152             }
153              
154             =head1 UTILITY METHODS
155              
156             These methods are defined in L and used here so they are
157             made available natively inside templates. See L for more
158             information.
159              
160             =head2 _p( $expr )
161              
162             =head2 _P( $expr )
163              
164             =head2 escape( $expr )
165              
166             =head2 escape_xml( $expr )
167              
168             =head2 unescape_xml( $expr )
169              
170             =head2 encode_url( $url )
171              
172             =head2 decode_url( $url )
173              
174             =head2 checked( $expr )
175              
176             =head2 selected( $expr )
177              
178             =head2 disabled( $expr )
179              
180             =head2 nl2br( $text )
181              
182             =head2 text2html( $text )
183              
184             =head2 tagattr( $name, $expr, [$value] )
185              
186             =head2 tagattrs( %attrs )
187              
188             =head2 new_cycle( @items )
189              
190             =cut
191              
192             # this makes the Tenjin utility methods available to templates 'natively'
193             *_p = *Tenjin::Util::_p;
194             *_P = *Tenjin::Util::_P;
195             *escape = *Tenjin::Util::escape_xml;
196             *escape_xml = *Tenjin::Util::escape_xml;
197             *unescape_xml = *Tenjin::Util::unescape_xml;
198             *encode_url = *Tenjin::Util::encode_url;
199             *decode_url = *Tenjin::Util::decode_url;
200             *checked = *Tenjin::Util::checked;
201             *selected = *Tenjin::Util::selected;
202             *disabled = *Tenjin::Util::disabled;
203             *nl2br = *Tenjin::Util::nl2br;
204             *text2html = *Tenjin::Util::text2html;
205             *tagattr = *Tenjin::Util::tagattr;
206             *tagattrs = *Tenjin::Util::tagattrs;
207             *new_cycle = *Tenjin::Util::new_cycle;
208              
209             1;
210              
211             =head1 SEE ALSO
212              
213             L, L, L.
214              
215             =head1 AUTHOR
216              
217             The CPAN version of Tenjin was forked by Ido Perlmuter Eido at ido50.netE
218             from version 0.0.2 of the original plTenjin, which is developed by Makoto Kuwata
219             at L.
220              
221             Development of Tenjin is done with github at L.
222              
223             =head1 LICENSE AND COPYRIGHT
224              
225             Tenjin is licensed under the MIT license.
226              
227             Copyright (c) 2007-2010 the aforementioned authors.
228              
229             Permission is hereby granted, free of charge, to any person obtaining
230             a copy of this software and associated documentation files (the
231             "Software"), to deal in the Software without restriction, including
232             without limitation the rights to use, copy, modify, merge, publish,
233             distribute, sublicense, and/or sell copies of the Software, and to
234             permit persons to whom the Software is furnished to do so, subject to
235             the following conditions:
236              
237             The above copyright notice and this permission notice shall be
238             included in all copies or substantial portions of the Software.
239              
240             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
241             EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
242             MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
243             NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
244             LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
245             OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
246             WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
247              
248             =cut