lib/Dancer2/Core/Role/Template.pm

filter: Statementstmt 95.9% 95 / 99 Branchbran 84.3% 27 / 32 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 92.3% 24 / 26 Podpod 77.7% 7 / 9 totaltotal 90.5% 163 / 180 CC 21 SCAR 11.6
CC
21
Cov
92%
apply_layout
7.3
process
6.0
_prepare_tokens_options
4.0
CRAP 3.2
« blib/lib/Dancer2/Serializer/Mutable.pm ↑ Summary lib/Dancer2/ConfigUtils.pm »
1 # ABSTRACT: Role for template engines
2  
3 package Dancer2::Core::Role::Template;
4 150 $Dancer2::Core::Role::Template::VERSION = '2.3.0';
5 150 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 150 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 150 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 150 ▶use Ref::Util qw< is_ref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 150 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 150 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 150 with 'Dancer2::Core::Role::Engine';
13  
14 180 ▶sub hook_aliases {
Subroutine hook_aliases: called CC=1 SCAR=0.0
Pod undocumented
15 {
16 180 before_template_render => 'engine.template.before_render',
17 after_template_render => 'engine.template.after_render',
18 before_layout_render => 'engine.template.before_layout_render',
19 after_layout_render => 'engine.template.after_layout_render',
20 }
21 }
22  
23 45 ▶sub supported_hooks { values %{ shift->hook_aliases } }
Subroutine supported_hooks: called CC=1 SCAR=0.0
Pod undocumented
24  
25 0 ▶sub _build_type {'Template'}
Subroutine _build_type: not called CC=1 SCAR=6.9
26  
27 150 requires 'render';
28  
29 has log_cb => (
30 is => 'ro',
31 isa => CodeRef,
32 0 ▶ default => sub { sub {1} },
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
33 150 );
34  
35 150 has name => (
36 is => 'ro',
37 lazy => 1,
38 builder => 1,
39 );
40  
41 2 ▶sub _build_name {
Subroutine _build_name: called CC=1 SCAR=0.0
42 2 ( my $name = ref shift ) =~ s/^Dancer2::Template:://;
43 2 $name;
44 }
45  
46 has charset => (
47 is => 'ro',
48 isa => Str,
49 11 ▶ default => sub {'UTF-8'},
Subroutine __ANON__: called CC=1 SCAR=0.0
50 150 );
51  
52 has default_tmpl_ext => (
53 is => 'ro',
54 isa => Str,
55 228 ▶ default => sub { shift->config->{extension} || 'tt' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->config->{"extension"} or "tt" 1 227 228
56 150 );
57  
58 150 has engine => (
59 is => 'ro',
60 isa => Object,
61 lazy => 1,
62 builder => 1,
63 );
64  
65 has settings => (
66 is => 'ro',
67 isa => HashRef,
68 lazy => 1,
69 4 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 150 writer => 'set_settings',
71 );
72  
73 # The attributes views, layout and layout_dir have triggers in
74 # Dancer2::Core::App that enable their values to be modified by
75 # the `set` keyword. As such, these are defined as read-write attrs.
76  
77 has views => (
78 is => 'rw',
79 isa => Maybe [Str],
80 239 ▶ trigger => sub { $_[0]->_clear_views_path },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 150 );
82  
83 150 has layout => (
84 is => 'rw',
85 isa => Maybe [Str],
86 );
87  
88 150 has layout_dir => (
89 is => 'rw',
90 isa => Maybe [Str],
91 );
92  
93 150 has _views_path => (
94 is => 'rw',
95 lazy => 1,
96 builder => '_build_views_path',
97 clearer => '_clear_views_path',
98 init_arg => undef,
99 );
100  
101 56 ▶sub _build_views_path {
Subroutine _build_views_path: called CC=1 SCAR=0.0
102 56 my $self = shift;
103 56 return Path::Tiny::path( $self->views );
104 }
105  
106 251 ▶sub _template_name {
Subroutine _template_name: called CC=2 SCAR=6.9
107 251 my ( $self, $view ) = @_;
108 251 my $def_tmpl_ext = $self->default_tmpl_ext();
109 251 ▶ $view .= ".$def_tmpl_ext" if $view !~ /\.\Q$def_tmpl_ext\E$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $view =~ /\.\Q$def_tmpl_ext\E$/ 248 3 251
110 251 return $view;
111 }
112  
113 178 ▶sub view_pathname {
Subroutine view_pathname: called CC=1 SCAR=0.0
Pod documented
114 178 my ( $self, $view ) = @_;
115  
116 178 $view = $self->_template_name($view);
117 178 return $self->_views_path->child($view)->stringify;
118 }
119  
120 3 ▶sub layout_pathname {
Subroutine layout_pathname: called CC=2 SCAR=7.1
Pod documented
121 3 my ( $self, $layout ) = @_;
122  
123 3 ▶ my @parts = defined $self->layout_dir ? ( $self->layout_dir ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->layout_dir ? : 3 0 3
124 3 return $self->_views_path->child(
125 @parts,
126 $self->_template_name($layout),
127 )->stringify;
128 }
129  
130 155 ▶sub pathname_exists {
Subroutine pathname_exists: called CC=2 SCAR=7.1
Pod documented
131 155 my ( $self, $pathname ) = @_;
132 155 ▶ return $pathname->is_file if blessed($pathname);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($pathname) 0 155 155
133 155 return Path::Tiny::path($pathname)->is_file;
134 }
135  
136 12 ▶sub render_layout {
Subroutine render_layout: called CC=1 SCAR=0.0
Pod documented
137 12 my ( $self, $layout, $tokens, $content ) = @_;
138  
139 12 $layout = $self->layout_pathname($layout);
140  
141 # FIXME: not sure if I can "just call render"
142 12 $self->render( $layout, { %$tokens, content => $content } );
143 }
144  
145 60 ▶sub apply_renderer {
Subroutine apply_renderer: called CC=3 SCAR=11.1
Pod documented
146 60 my ( $self, $view, $tokens ) = @_;
147 60 ▶ $view = $self->view_pathname($view) if !is_ref($view);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ref($view) 57 3 60
148 60 $tokens = $self->_prepare_tokens_options( $tokens );
149  
150 60 $self->execute_hook( 'engine.template.before_render', $tokens );
151  
152 56 my $content = $self->render( $view, $tokens );
153 51 $self->execute_hook( 'engine.template.after_render', \$content );
154  
155 # make sure to avoid ( undef ) in list context return
156 51 ▶ defined $content and return $content;
Branch: 1 branch
BranchTrueFalseTotal
defined $content and return $content 51 0 51
157 0 return;
158 }
159  
160 51 ▶sub apply_layout {
Subroutine apply_layout: called CC=7 SCAR=19.8
Pod documented
161 51 my ( $self, $content, $tokens, $options ) = @_;
162  
163 51 $tokens = $self->_prepare_tokens_options( $tokens );
164  
165 # If 'layout' was given in the options hashref, use it if it's a true value,
166 # or don't use a layout if it was false (0, or undef); if layout wasn't
167 # given in the options hashref, go with whatever the current layout setting
168 # is.
169 my $layout =
170 exists $options->{layout}
171 ? ( $options->{layout} ? $options->{layout} : undef )
172 51 ▶ : ( $self->layout || $self->config->{layout} );
Branch: 2 branches
BranchTrueFalseTotal
exists $options->{'layout'} ? : 9 42 51
$options->{'layout'} ? : 1 8 9
Condition66%2/3
$self->layout or $self->config->{"layout"}
l!l&&r!l&&!r
11031
MC/DC50%1/2
$self->layout or $self->config->{"layout"}
$self->layout $self->config->{"layout"}
Truth table66%2/3
A or B
A= $self->layoutB= $self->config->{"layout"}
ABresult
1X1
011
000
173  
174 # that should only be $self->config, but the layout ain't there ???
175  
176 51 ▶ defined $content or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $content or return 51 0 51
177 51 ▶ defined $layout or return $content;
Branch: 1 branch
BranchTrueFalseTotal
defined $layout or return $content 12 39 51
178  
179 12 $self->execute_hook(
180 'engine.template.before_layout_render',
181 $tokens, \$content
182 );
183  
184 12 my $full_content = $self->render_layout( $layout, $tokens, $content );
185  
186 12 $self->execute_hook( 'engine.template.after_layout_render',
187 \$full_content );
188  
189 # make sure to avoid ( undef ) in list context return
190 12 ▶ defined $full_content and return $full_content;
Branch: 1 branch
BranchTrueFalseTotal
defined $full_content and return $full_content 12 0 12
191 0 return;
192 }
193  
194 111 ▶sub _prepare_tokens_options {
Subroutine _prepare_tokens_options: called CC=4 SCAR=13.9
195 111 my ( $self, $tokens ) = @_;
196  
197 # these are the default tokens provided for template processing
198 111 ▶ $tokens ||= {};
Condition50%1/2
$tokens ||= {}
l!l
1110
MC/DC0%0/1
$tokens ||= {}
$tokens
Truth table50%1/2
A
A= $tokens
Aresult
11
00
199 111 $tokens->{perl_version} = $^V;
200 111 $tokens->{dancer_version} = Dancer2->VERSION;
201 111 $tokens->{settings} = $self->settings;
202  
203 # no request when template is called as a global keyword
204 111 ▶ if ( $self->has_request ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_request) 105 6 111
205 105 $tokens->{request} = $self->request;
206 105 $tokens->{params} = $self->request->params;
207 105 $tokens->{vars} = $self->request->vars;
208  
209 # a session can not exist if there is no request
210 105 ▶ $tokens->{session} = $self->session->data
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_session 14 91 105
211 if $self->has_session;
212 }
213  
214 111 return $tokens;
215 }
216  
217 61 ▶sub process {
Subroutine process: called CC=6 SCAR=17.9
Pod documented
218 61 my ( $self, $view, $tokens, $options ) = @_;
219 61 my ( $content, $full_content );
220  
221 # it's important that $tokens is not undef, so that things added to it via
222 # a before_template in apply_renderer survive to the apply_layout. GH#354
223 61 ▶ $tokens ||= {};
Condition100%2/2
$tokens ||= {}
l!l
3427
MC/DC100%1/1
$tokens ||= {}
$tokens
Truth table100%2/2
A
A= $tokens
Aresult
11
00
224 61 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
1249
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
225  
226 ## FIXME - Look into PR 654 so we fix the problem here as well!
227  
228 $content =
229 $view
230 ? $self->apply_renderer( $view, $tokens )
231 61 ▶ : delete $options->{content};
Branch: 1 branch
BranchTrueFalseTotal
$view ? : 60 1 61
232  
233 52 ▶ defined $content
Branch: 1 branch
BranchTrueFalseTotal
defined $content and $full_content = $self->apply_layout($content, $tokens, $options) 51 1 52
234 and $full_content = $self->apply_layout( $content, $tokens, $options );
235  
236 52 ▶ defined $full_content
Branch: 1 branch
BranchTrueFalseTotal
defined $full_content and return $full_content 51 1 52
237 and return $full_content;
238  
239 1 croak "Template did not produce any content";
240 }
241  
242 150 1;
243  
244 __END__
« blib/lib/Dancer2/Serializer/Mutable.pm ↑ Summary lib/Dancer2/ConfigUtils.pm »