View/TT.pm

filter: Statementstmt 86.8% 139 / 160 Branchbran 66.1% 45 / 68 Conditioncond 54.5% 12 / 22 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 76.7% 218 / 284 CC 45 SCAR 25.4
CC
45
Cov
78%
new
27.7
process
10.9
render
10.8
CRAP 12.7
↑ Summary Helper/View/TTSite.pm »
1 package Catalyst::View::TT;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use base qw/Catalyst::View/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Data::Dump 'dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Template::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Scalar::Util qw/blessed weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 our $VERSION = '0.46';
14 12 $VERSION =~ tr/_//d;
15  
16 12 __PACKAGE__->mk_accessors('template');
17 12 __PACKAGE__->mk_accessors('expose_methods');
18 12 __PACKAGE__->mk_accessors('include_path');
19 12 __PACKAGE__->mk_accessors('content_type');
20  
21 12 *paths = \&include_path;
22  
23 =head1 NAME
24  
25 Catalyst::View::TT - Template View Class
26  
27 =head1 SYNOPSIS
28  
29 # use the helper to create your View
30  
31 myapp_create.pl view Web TT
32  
33 # add custom configuration in View/Web.pm
34  
35 __PACKAGE__->config(
36 # any TT configuration items go here
37 TEMPLATE_EXTENSION => '.tt',
38 CATALYST_VAR => 'c',
39 TIMER => 0,
40 ENCODING => 'utf-8'
41 # Not set by default
42 PRE_PROCESS => 'config/main',
43 WRAPPER => 'site/wrapper',
44 render_die => 1, # Default for new apps, see render method docs
45 expose_methods => [qw/method_in_view_class/],
46 );
47  
48 # add include path configuration in MyApp.pm
49  
50 __PACKAGE__->config(
51 'View::Web' => {
52 INCLUDE_PATH => [
53 __PACKAGE__->path_to( 'root', 'src' ),
54 __PACKAGE__->path_to( 'root', 'lib' ),
55 ],
56 },
57 );
58  
59 # render view from lib/MyApp.pm or lib/MyApp::Controller::SomeController.pm
60  
61 sub message : Global {
62 my ( $self, $c ) = @_;
63 $c->stash->{template} = 'message.tt2';
64 $c->stash->{message} = 'Hello World!';
65 $c->forward( $c->view('Web') );
66 }
67  
68 # access variables from template
69  
70 The message is: [% message %].
71  
72 # example when CATALYST_VAR is set to 'Catalyst'
73 Context is [% Catalyst %]
74 The base is [% Catalyst.req.base %]
75 The name is [% Catalyst.config.name %]
76  
77 # example when CATALYST_VAR isn't set
78 Context is [% c %]
79 The base is [% base %]
80 The name is [% name %]
81  
82 =cut
83  
84 92 ▶sub _coerce_paths {
Subroutine _coerce_paths: called CC=5 SCAR=17.0
85 92 my ( $paths, $dlim ) = shift;
86 92 ▶ return () if ( !$paths );
Branch: 1 branch
BranchTrueFalseTotal
unless $paths 71 21 92
87 0 ▶ return @{$paths} if ( ref $paths eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $paths eq "ARRAY" 0 21 21
88  
89 # tweak delim to ignore C:/
90 21 ▶ unless ( defined $dlim ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dlim) 21 0 21
91 21 ▶ $dlim = ( $^O eq 'MSWin32' ) ? ':(?!\\/)' : ':';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 21 21
92 }
93 21 return split( /$dlim/, $paths );
94 }
95  
96 112 ▶sub new {
Subroutine new: called CC=17 SCAR=33.2
Pod documented
97 112 my ( $class, $c, $arguments ) = @_;
98 my $config = {
99 EVAL_PERL => 0,
100 TEMPLATE_EXTENSION => '',
101 CLASS => 'Template',
102 112 %{ $class->config },
103 112 %{$arguments},
104 };
105 112 ▶ if ( ! (ref $config->{INCLUDE_PATH} eq 'ARRAY') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config->{"INCLUDE_PATH"} eq "ARRAY") 92 20 112
106 92 my $delim = $config->{DELIMITER};
107 my @include_path
108 92 = _coerce_paths( $config->{INCLUDE_PATH}, $delim );
109 92 ▶ if ( !@include_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@include_path) 71 21 92
110 71 my $root = $c->config->{root};
111 71 my $base = Path::Class::dir( $root, 'base' );
112 71 @include_path = ( "$root", "$base" );
113 }
114 92 $config->{INCLUDE_PATH} = \@include_path;
115 }
116  
117 # if we're debugging and/or the TIMER option is set, then we install
118 # Template::Timer as a custom CONTEXT object, but only if we haven't
119 # already got a custom CONTEXT defined
120  
121 112 ▶ if ( $config->{TIMER} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"TIMER"}) 0 112 112
122 0 ▶ if ( $config->{CONTEXT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'CONTEXT'}) { } 0 0 0
123 0 $c->log->error(
124 'Cannot use Template::Timer - a TT CONTEXT is already defined'
125 );
126 }
127 else {
128 0 $config->{CONTEXT} = Template::Timer->new(%$config);
129 }
130 }
131  
132 112 ▶ if ( $c->debug && $config->{DUMP_CONFIG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug and $config->{"DUMP_CONFIG"}) 0 112 112
Condition33%1/3
$c->debug and $config->{"DUMP_CONFIG"}
!ll&&!rl&&r
11200
MC/DC0%0/2
$c->debug and $config->{"DUMP_CONFIG"}
$c->debug $config->{"DUMP_CONFIG"}
Truth table33%1/3
A and B
A= $c->debugB= $config->{"DUMP_CONFIG"}
ABresult
0X0
100
111
133 0 $c->log->debug( "TT Config: ", dump($config) );
134 }
135  
136 112 my $self = $class->next::method(
137 $c, { %$config },
138 );
139  
140 # Set base include paths. Local'd in render if needed
141 112 $self->include_path($config->{INCLUDE_PATH});
142  
143 112 $self->expose_methods($config->{expose_methods});
144 112 $self->config($config);
145  
146 # Creation of template outside of call to new so that we can pass [ $self ]
147 # as INCLUDE_PATH config item, which then gets ->paths() called to get list
148 # of include paths to search for templates.
149  
150 # Use a weakened copy of self so we don't have loops preventing GC from working
151 112 my $copy = $self;
152 112 Scalar::Util::weaken($copy);
153 112 ▶ $config->{INCLUDE_PATH} = [ sub { $copy->paths } ];
Subroutine __ANON__: called CC=1 SCAR=0.0
154  
155 112 ▶ if ( $config->{PROVIDERS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"PROVIDERS"}) 10 102 112
156 10 my @providers = ();
157 10 ▶ if ( ref($config->{PROVIDERS}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config->{"PROVIDERS"} eq "ARRAY") 10 0 10
158 10 foreach my $p (@{$config->{PROVIDERS}}) {
159 10 my $pname = $p->{name};
160 10 my $prov = 'Template::Provider';
161 10 ▶ if($pname eq '_file_')
Branch: 1 branch
BranchTrueFalseTotal
if ($pname eq '_file_') { } 0 10 10
162 {
163 0 $p->{args} = { %$config };
164 }
165 else
166 {
167 10 ▶ if($pname =~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pname =~ s/^\+//) { } 10 0 10
168 10 $prov = $pname;
169 }
170 else
171 {
172 0 $prov .= "::$pname";
173 }
174 # We copy the args people want from the config
175 # to the args
176 10 ▶ $p->{args} ||= {};
Condition50%1/2
$p->{"args"} ||= {}
l!l
100
MC/DC0%0/1
$p->{"args"} ||= {}
$p->{"args"}
Truth table50%1/2
A
A= $p->{"args"}
Aresult
11
00
177 10 ▶ if ($p->{copy_config}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"copy_config"}) 0 10 10
178 0 map { $p->{args}->{$_} = $config->{$_} }
179 0 grep { exists $config->{$_} }
180 0 @{ $p->{copy_config} };
181 }
182 }
183 10 local $@;
184 10 eval "require $prov";
185 10 ▶ if(!$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 10 0 10
186 10 push @providers, "$prov"->new($p->{args});
187 }
188 else
189 {
190 0 $c->log->warn("Can't load $prov, ($@)");
191 }
192 }
193 }
194 10 delete $config->{PROVIDERS};
195 10 ▶ if(@providers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@providers) 10 0 10
196 10 $config->{LOAD_TEMPLATES} = \@providers;
197 }
198 }
199  
200 $self->{template} =
201 112 ▶ $config->{CLASS}->new($config) || do {
Condition33%1/3
$config->{'CLASS'}->new($config) || do { my $error = $config->{'CLASS'}->error; $c->log->error($error); $c->error($error); return undef }
l!l&&r!l&&!r
11200
MC/DC0%0/2
$config->{'CLASS'}->new($config) || do { my $error = $config->{'CLASS'}->error; $c->log->error($error); $c->error($error); return undef }
$config->{'CLASS'}->new($config) do { my $error = $config->{'CLASS'}->error; $c->log->error($error); $c->error($error); return undef }
Truth table33%1/3
A || B
A= $config->{'CLASS'}->new($config)B= do { my $error = $config->{'CLASS'}->error; $c->log->error($error); $c->error($error); return undef }
ABresult
1X1
011
000
202 0 my $error = $config->{CLASS}->error();
203 0 $c->log->error($error);
204 0 $c->error($error);
205 0 return undef;
206 };
207  
208  
209 112 return $self;
210 }
211  
212 20 ▶sub process {
Subroutine process: called CC=9 SCAR=23.9
Pod documented
213 20 my ( $self, $c ) = @_;
214  
215 my $template = $c->stash->{template}
216 20 ▶ || $c->action . $self->config->{TEMPLATE_EXTENSION};
Condition66%2/3
$c->stash->{'template'} || $c->action . $self->config->{'TEMPLATE_EXTENSION'}
l!l&&r!l&&!r
1280
MC/DC0%0/2
$c->stash->{'template'} || $c->action . $self->config->{'TEMPLATE_EXTENSION'}
$c->stash->{'template'} $c->action . $self->config->{'TEMPLATE_EXTENSION'}
Truth table66%2/3
A || B
A= $c->stash->{'template'}B= $c->action . $self->config->{'TEMPLATE_EXTENSION'}
ABresult
1X1
011
000
217  
218 20 ▶ unless (defined $template) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $template) 0 20 20
219 0 ▶ $c->log->debug('No template specified for rendering') if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
220 0 return 0;
221 }
222  
223 20 local $@;
224 20 my $output = eval { $self->render($c, $template) };
225 20 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 20 20
226 0 return $self->_rendering_error($c, $template . ': ' . $err);
227 }
228 20 ▶ if (blessed($output) && $output->isa('Template::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($output) and $output->isa("Template::Exception")) 2 18 20
Condition66%2/3
builtin::blessed($output) and $output->isa("Template::Exception")
!ll&&!rl&&r
1802
MC/DC50%1/2
builtin::blessed($output) and $output->isa("Template::Exception")
builtin::blessed($output) $output->isa("Template::Exception")
Truth table66%2/3
A and B
A= builtin::blessed($output)B= $output->isa("Template::Exception")
ABresult
0X0
100
111
229 2 $self->_rendering_error($c, $output);
230 }
231  
232 20 ▶ unless ( $c->response->content_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->response->content_type) 20 0 20
233 20 ▶ my $default = $self->content_type || 'text/html; charset=UTF-8';
Condition100%2/2
$self->content_type || 'text/html; charset=UTF-8'
l!l
119
MC/DC100%1/1
$self->content_type || 'text/html; charset=UTF-8'
$self->content_type
Truth table100%2/2
A
A= $self->content_type
Aresult
11
00
234 20 $c->response->content_type($default);
235 }
236  
237 20 $c->response->body($output);
238  
239 20 return 1;
240 }
241  
242 2 ▶sub _rendering_error {
Subroutine _rendering_error: called CC=1 SCAR=0.0
243 2 my ($self, $c, $err) = @_;
244 2 my $error = qq/Couldn't render template "$err"/;
245 2 $c->log->error($error);
246 2 $c->error($error);
247 2 return 0;
248 }
249  
250 24 ▶sub render {
Subroutine render: called CC=10 SCAR=23.8
Pod documented
251 24 my ($self, $c, $template, $args) = @_;
252  
253 24 ▶ $c->log->debug(qq/Rendering template "$template"/) if $c && $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c and $c->debug 0 24 24
Condition66%2/3
$c and $c->debug
!ll&&!rl&&r
1230
MC/DC0%0/2
$c and $c->debug
$c $c->debug
Truth table66%2/3
A and B
A= $cB= $c->debug
ABresult
0X0
100
111
254  
255 24 my $output;
256 my $vars = {
257 24 ▶ (ref $args eq 'HASH' ? %$args : %{ $c->stash() }),
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 3 21 24
258 $self->template_vars($c)
259 };
260  
261 local $self->{include_path} =
262 2 [ @{ $vars->{additional_template_paths} }, @{ $self->{include_path} } ]
263 24 ▶ if ref $vars->{additional_template_paths};
Branch: 1 branch
BranchTrueFalseTotal
if ref $vars->{"additional_template_paths"} 2 22 24
264  
265 24 ▶ unless ( $self->template->process( $template, $vars, \$output ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->template->process($template, $vars, \$output)) 3 21 24
266 3 ▶ if (exists $self->{render_die}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"render_die"}) 1 2 3
267 1 ▶ die $self->template->error if $self->{render_die};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"render_die"} 1 0 1
268 0 return $self->template->error;
269 }
270 2 ▶ $c->log->debug('The Catalyst::View::TT render() method will start dying on error in a future release. Unless you are calling the render() method manually, you probably want the new behaviour, so set render_die => 1 in config for ' . blessed($self) . '. If you wish to continue to return the exception rather than throwing it, add render_die => 0 to your config.') if $c && $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c and $c->debug 0 2 2
Condition33%1/3
$c and $c->debug
!ll&&!rl&&r
020
MC/DC0%0/2
$c and $c->debug
$c $c->debug
Truth table33%1/3
A and B
A= $cB= $c->debug
ABresult
0X0
100
111
271 2 return $self->template->error;
272 }
273 21 return $output;
274 }
275  
276 24 ▶sub template_vars {
Subroutine template_vars: called CC=6 SCAR=17.9
Pod documented
277 24 my ( $self, $c ) = @_;
278  
279 24 ▶ return () unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 1 23 24
280 23 my $cvar = $self->config->{CATALYST_VAR};
281  
282 my %vars = defined $cvar
283 ? ( $cvar => $c )
284 : (
285 c => $c,
286 base => $c->req->base,
287 name => $c->config->{name}
288 23 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $cvar ? : 0 23 23
289  
290 23 ▶ if ($self->expose_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->expose_methods) 4 19 23
291 4 my $meta = $self->meta;
292 4 foreach my $method_name (@{$self->expose_methods}) {
293 8 my $method = $meta->find_method_by_name( $method_name );
294 8 ▶ unless ($method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 8 8
295 0 Catalyst::Exception->throw( "$method_name not found in TT view" );
296 }
297 8 my $method_body = $method->body;
298 8 my $weak_ctx = $c;
299 8 weaken $weak_ctx;
300 4 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
301 4 my @args = @_;
302 4 my $ret;
303 4 eval {
304 4 $ret = $self->$method_body($weak_ctx, @args);
305 };
306 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 2 4
307 2 ▶ if (blessed($@)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@)) { } 1 1 2
308 1 die $@;
309 } else {
310 1 Catalyst::Exception->throw($@);
311 }
312 }
313 2 return $ret;
314 8 };
315 8 $vars{$method_name} = $sub;
316 }
317 }
318 23 return %vars;
319 }
320  
321 12 1;
322  
323 __END__
↑ Summary Helper/View/TTSite.pm »