lib/Dancer/Template/Abstract.pm

filter: Statementstmt 88.9% 97 / 109 Branchbran 71.8% 23 / 32 Conditioncond 87.5% 14 / 16 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 90.9% 20 / 22 Podpod 62.5% 5 / 8 totaltotal 84.1% 165 / 196 CC 26 SCAR 13.9
CC
26
Cov
85%
template
8.3
apply_layout
6.5
_prepare_tokens_options
5.0
CRAP 4.0
« blib/lib/Dancer/Session/YAML.pm ↑ Summary lib/Dancer/Route/Registry.pm »
1 package Dancer::Template::Abstract;
2 245 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: abstract class for Dancer's template engines
4 245 $Dancer::Template::Abstract::VERSION = '1.3522';
5 245 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 245 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 245 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 245 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 245 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 245 ▶use Dancer::FileUtils 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 245 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 245 ▶use base 'Dancer::Engine';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 245 Dancer::Factory::Hook->instance->install_hooks(
17 qw/before_template_render after_template_render before_layout_render after_layout_render/
18 );
19  
20 # overloads this method to implement the rendering
21 # args: $self, $template, $tokens
22 # return: a string of $template's content processed with $tokens
23 1 ▶sub render { confess "render not implemented" }
Subroutine render: called CC=1 SCAR=0.0
Pod documented
24  
25 110 ▶sub default_tmpl_ext { "tt" }
Subroutine default_tmpl_ext: called CC=1 SCAR=0.0
Pod documented
26  
27 # Work out the template names to look for; this will be the view name passed
28 # as-is, and also the view name with the default template extension appended, if
29 # it did not already end in that.
30 112 ▶sub _template_name {
Subroutine _template_name: called CC=3 SCAR=11.0
31 112 my ( $self, $view ) = @_;
32 112 my @views = ( $view );
33 112 ▶ my $def_tmpl_ext = $self->config->{extension} || $self->default_tmpl_ext();
Condition66%2/3
$self->config->{'extension'} || $self->default_tmpl_ext
l!l&&r!l&&!r
31090
MC/DC0%0/2
$self->config->{'extension'} || $self->default_tmpl_ext
$self->config->{'extension'} $self->default_tmpl_ext
Truth table66%2/3
A || B
A= $self->config->{'extension'}B= $self->default_tmpl_ext
ABresult
1X1
011
000
34 112 ▶ push @views, $view .= ".$def_tmpl_ext" if $view !~ /\.\Q$def_tmpl_ext\E$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $view =~ /\.\Q$def_tmpl_ext\E$/ 94 18 112
35 112 return @views;
36 }
37  
38 106 ▶sub view {
Subroutine view: called CC=3 SCAR=11.0
Pod documented
39 106 my ($self, $view) = @_;
40  
41 106 my $views_dir = Dancer::App->current->setting('views');
42  
43 106 for my $template ($self->_template_name($view)) {
44 194 my $view_path = path($views_dir, $template);
45 194 ▶ return $view_path if -f $view_path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $view_path 101 93 194
46 }
47  
48 # No matching view path was found
49 5 return;
50 }
51  
52 5 ▶sub layout {
Subroutine layout: called CC=4 SCAR=14.1
Pod documented
53 5 my ($self, $layout, $tokens, $content) = @_;
54  
55 5 my $layouts_dir = path(Dancer::App->current->setting('views'), 'layouts');
56 5 my $layout_path;
57 5 for my $layout_name ($self->_template_name($layout)) {
58 10 $layout_path = path($layouts_dir, $layout_name);
59 10 ▶ last if -e $layout_path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $layout_path 5 5 10
60 }
61  
62 5 my $full_content;
63 5 ▶ if (-e $layout_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $layout_path) { } 5 0 5
64 5 $full_content = Dancer::Template->engine->render(
65 $layout_path, {%$tokens, content => $content});
66 } else {
67 0 $full_content = $content;
68 0 Dancer::Logger::error("Defined layout ($layout) was not found!");
69 }
70 5 $full_content;
71 }
72  
73 56 ▶sub apply_renderer {
Subroutine apply_renderer: called CC=2 SCAR=7.6
Pod undocumented
74 56 my ($self, $view, $tokens) = @_;
75  
76 56 ($tokens, undef) = _prepare_tokens_options($tokens);
77  
78 56 $view = $self->view($view);
79  
80 56 Dancer::Factory::Hook->execute_hooks('before_template_render', $tokens);
81  
82 45 my $content;
83 45 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 45 $content = $self->render($view, $tokens);
85 0 ▶ } continuation {
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 my ($continuation) = @_;
87 # If we have a Route continuation, run the after hook, then
88 # propagate the continuation
89 0 Dancer::Factory::Hook->execute_hooks('after_template_render', \$content);
90 0 $continuation->rethrow();
91 45 };
92  
93 45 Dancer::Factory::Hook->execute_hooks('after_template_render', \$content);
94  
95 # make sure to avoid ( undef ) in list context return
96 42 ▶ defined $content
Branch: 1 branch
BranchTrueFalseTotal
defined $content and return $content 42 0 42
97 and return $content;
98 0 return;
99 }
100  
101 27 ▶sub apply_layout {
Subroutine apply_layout: called CC=6 SCAR=18.7
Pod undocumented
102 27 my ($self, $content, $tokens, $options) = @_;
103  
104 27 ($tokens, $options) = _prepare_tokens_options($tokens, $options);
105  
106 # If 'layout' was given in the options hashref, use it if it's a true value,
107 # or don't use a layout if it was false (0, or undef); if layout wasn't
108 # given in the options hashref, go with whatever the current layout setting
109 # is.
110 my $layout =
111 exists $options->{layout}
112 ? ($options->{layout} ? $options->{layout} : undef)
113 27 ▶ : Dancer::App->current->setting('layout');
Branch: 2 branches
BranchTrueFalseTotal
exists $options->{'layout'} ? : 4 23 27
$options->{'layout'} ? : 2 2 4
114  
115 27 ▶ defined $content or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $content or return 27 0 27
116  
117 27 ▶ defined $layout or return $content;
Branch: 1 branch
BranchTrueFalseTotal
defined $layout or return $content 5 22 27
118  
119 5 Dancer::Factory::Hook->execute_hooks('before_layout_render', $tokens, \$content);
120  
121 5 my $full_content;
122  
123 5 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 5 $full_content = $self->layout($layout, $tokens, $content);
125 0 ▶ } continuation {
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 my ($continuation) = @_;
127 # If we have a Route continuation, run the after hook, then
128 # propagate the continuation
129 0 Dancer::Factory::Hook->execute_hooks('after_layout_render', \$full_content);
130 0 $continuation->rethrow();
131 5 };
132  
133 5 Dancer::Factory::Hook->execute_hooks('after_layout_render', \$full_content);
134  
135 # make sure to avoid ( undef ) in list context return
136 5 ▶ defined $full_content
Branch: 1 branch
BranchTrueFalseTotal
defined $full_content and return $full_content 5 0 5
137 and return $full_content;
138 0 return;
139 }
140  
141 83 ▶sub _prepare_tokens_options {
Subroutine _prepare_tokens_options: called CC=5 SCAR=16.1
142 83 my ($tokens, $options) = @_;
143  
144 83 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
2657
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
145  
146 # these are the default tokens provided for template processing
147 83 ▶ $tokens ||= {};
Condition100%2/2
$tokens ||= {}
l!l
821
MC/DC100%1/1
$tokens ||= {}
$tokens
Truth table100%2/2
A
A= $tokens
Aresult
11
00
148 83 $tokens->{perl_version} = $];
149 83 $tokens->{dancer_version} = $Dancer::VERSION;
150 83 $tokens->{settings} = Dancer::Config->settings;
151  
152 # If we're processing a request, also add the request object, params and
153 # vars as tokens:
154 83 ▶ if (my $request = Dancer::SharedData->request) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $request = "Dancer::SharedData"->request) 83 0 83
155 83 $tokens->{request} = $request;
156 83 $tokens->{params} = $request->params;
157 83 $tokens->{vars} = Dancer::SharedData->vars;
158 }
159  
160 Dancer::App->current->setting('session')
161 83 ▶ and $tokens->{session} = Dancer::Session->get;
Branch: 1 branch
BranchTrueFalseTotal
"Dancer::App"->current->setting("session") and $tokens->{"session"} = "Dancer::Session"->get 83 0 83
162  
163 83 return ($tokens, $options);
164 }
165  
166 39 ▶sub template {
Subroutine template: called CC=8 SCAR=21.2
Pod undocumented
167 39 my ($class, $view, $tokens, $options) = @_;
168 39 my ($content, $full_content);
169  
170 39 my $engine = Dancer::Template->engine;
171  
172 # it's important that $tokens is not undef, so that things added to it via
173 # a before_template in apply_renderer survive to the apply_layout. GH#354
174 39 ▶ $tokens ||= {};
Condition100%2/2
$tokens ||= {}
l!l
2415
MC/DC100%1/1
$tokens ||= {}
$tokens
Truth table100%2/2
A
A= $tokens
Aresult
11
00
175 39 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
237
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
176  
177 39 ▶ if ($view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($view) { } 39 0 39
178 # check if the requested view exists
179 39 ▶ my $view_path = $engine->view($view) || '';
Condition100%2/2
$engine->view($view) || ''
l!l
381
MC/DC100%1/1
$engine->view($view) || ''
$engine->view($view)
Truth table100%2/2
A
A= $engine->view($view)
Aresult
11
00
180 39 ▶ if ($engine->view_exists($view_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine->view_exists($view_path)) { } 38 1 39
181 38 $content = $engine->apply_renderer($view, $tokens);
182 } else {
183 1 Dancer::Logger::error(
184 "Supplied view ($view) not found - $view_path does not exist"
185 );
186 1 return Dancer::Error->new(
187 code => 500,
188 message => 'view not found',
189 )->render();
190 }
191 } else {
192 0 $content = delete $options->{content};
193 }
194  
195 24 ▶ defined $content and $full_content =
Branch: 1 branch
BranchTrueFalseTotal
defined $content and $full_content = $engine->apply_layout($content, $tokens, $options) 24 0 24
196 $engine->apply_layout($content, $tokens, $options);
197  
198 24 ▶ defined $full_content
Branch: 1 branch
BranchTrueFalseTotal
defined $full_content and return $full_content 24 0 24
199 and return $full_content;
200  
201 0 Dancer::Error->new(
202 code => 404,
203 message => "Page not found",
204 )->render();
205 }
206  
207 39 ▶sub view_exists { return defined $_[1] && -f $_[1] }
Subroutine view_exists: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
defined $_[1] && -f $_[1]
!ll&&!rl&&r
0138
MC/DC50%1/2
defined $_[1] && -f $_[1]
defined $_[1] -f $_[1]
Truth table66%2/3
A && B
A= defined $_[1]B= -f $_[1]
ABresult
0X0
100
111
208  
209 245 1;
210  
211 __END__
« blib/lib/Dancer/Session/YAML.pm ↑ Summary lib/Dancer/Route/Registry.pm »