blib/lib/Dancer2/Core/Role/Template.pm

filter: Statementstmt 96.9% 96 / 99 Branchbran 84.3% 27 / 32 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 96.0% 24 / 25 Podpod 77.7% 7 / 9 totaltotal 91.6% 164 / 179 CC 21 SCAR 11.8
CC
21
Cov
93%
apply_layout
7.3
process
6.0
_prepare_tokens_options
4.0
CRAP 3.2
« blib/lib/Dancer2/Serializer/Mutable.pm ↑ Summary blib/lib/Dancer2/ConfigUtils.pm »
1 # ABSTRACT: Role for template engines
2  
3 package Dancer2::Core::Role::Template;
4 146 $Dancer2::Core::Role::Template::VERSION = '2.2.1';
5 146 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 146 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 146 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 146 ▶use Ref::Util qw< is_ref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 146 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 146 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 146 with 'Dancer2::Core::Role::Engine';
13  
14 sub hook_aliases {
15 {
16 175 ▶ before_template_render => 'engine.template.before_render',
Subroutine hook_aliases: called CC=1 SCAR=0.0
Pod undocumented
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 44 ▶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 146 requires 'render';
28  
29 has log_cb => (
30 is => 'ro',
31 isa => CodeRef,
32 5 ▶ default => sub { sub {1} },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 146 );
34  
35 146 has name => (
36 is => 'ro',
37 lazy => 1,
38 builder => 1,
39 );
40  
41 sub _build_name {
42 2 ▶ ( my $name = ref shift ) =~ s/^Dancer2::Template:://;
Subroutine _build_name: called CC=1 SCAR=0.0
43 2 $name;
44 }
45  
46 has charset => (
47 is => 'ro',
48 isa => Str,
49 10 ▶ default => sub {'UTF-8'},
Subroutine __ANON__: called CC=1 SCAR=0.0
50 146 );
51  
52 has default_tmpl_ext => (
53 is => 'ro',
54 isa => Str,
55 224 ▶ default => sub { shift->config->{extension} || 'tt' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->config->{"extension"} or "tt" 223 1 224
56 146 );
57  
58 146 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 146 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 236 ▶ trigger => sub { $_[0]->_clear_views_path },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 146 );
82  
83 146 has layout => (
84 is => 'rw',
85 isa => Maybe [Str],
86 );
87  
88 146 has layout_dir => (
89 is => 'rw',
90 isa => Maybe [Str],
91 );
92  
93 146 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 sub _build_views_path {
102 54 ▶ my $self = shift;
Subroutine _build_views_path: called CC=1 SCAR=0.0
103 54 return Path::Tiny::path( $self->views );
104 }
105  
106 sub _template_name {
107 248 ▶ my ( $self, $view ) = @_;
Subroutine _template_name: called CC=2 SCAR=6.9
108 248 my $def_tmpl_ext = $self->default_tmpl_ext();
109 248 ▶ $view .= ".$def_tmpl_ext" if $view !~ /\.\Q$def_tmpl_ext\E$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $view =~ /\.\Q$def_tmpl_ext\E$/ 245 3 248
110 248 return $view;
111 }
112  
113 sub view_pathname {
114 176 ▶ my ( $self, $view ) = @_;
Subroutine view_pathname: called CC=1 SCAR=0.0
Pod documented
115  
116 176 $view = $self->_template_name($view);
117 176 return $self->_views_path->child($view)->stringify;
118 }
119  
120 sub layout_pathname {
121 2 ▶ my ( $self, $layout ) = @_;
Subroutine layout_pathname: called CC=2 SCAR=7.1
Pod documented
122  
123 2 ▶ my @parts = defined $self->layout_dir ? ( $self->layout_dir ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->layout_dir ? : 2 0 2
124 2 return $self->_views_path->child(
125 @parts,
126 $self->_template_name($layout),
127 )->stringify;
128 }
129  
130 sub pathname_exists {
131 154 ▶ my ( $self, $pathname ) = @_;
Subroutine pathname_exists: called CC=2 SCAR=7.1
Pod documented
132 154 ▶ return $pathname->is_file if blessed($pathname);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($pathname) 0 154 154
133 154 return Path::Tiny::path($pathname)->is_file;
134 }
135  
136 sub render_layout {
137 11 ▶ my ( $self, $layout, $tokens, $content ) = @_;
Subroutine render_layout: called CC=1 SCAR=0.0
Pod documented
138  
139 11 $layout = $self->layout_pathname($layout);
140  
141 # FIXME: not sure if I can "just call render"
142 11 $self->render( $layout, { %$tokens, content => $content } );
143 }
144  
145 sub apply_renderer {
146 59 ▶ my ( $self, $view, $tokens ) = @_;
Subroutine apply_renderer: called CC=3 SCAR=11.1
Pod documented
147 59 ▶ $view = $self->view_pathname($view) if !is_ref($view);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ref($view) 56 3 59
148 59 $tokens = $self->_prepare_tokens_options( $tokens );
149  
150 59 $self->execute_hook( 'engine.template.before_render', $tokens );
151  
152 55 my $content = $self->render( $view, $tokens );
153 50 $self->execute_hook( 'engine.template.after_render', \$content );
154  
155 # make sure to avoid ( undef ) in list context return
156 50 ▶ defined $content and return $content;
Branch: 1 branch
BranchTrueFalseTotal
defined $content and return $content 50 0 50
157 0 return;
158 }
159  
160 sub apply_layout {
161 50 ▶ my ( $self, $content, $tokens, $options ) = @_;
Subroutine apply_layout: called CC=7 SCAR=19.8
Pod documented
162  
163 50 $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 50 ▶ : ( $self->layout || $self->config->{layout} );
Branch: 2 branches
BranchTrueFalseTotal
exists $options->{'layout'} ? : 9 41 50
$options->{'layout'} ? : 1 8 9
Condition66%2/3
$self->layout or $self->config->{"layout"}
l!l&&r!l&&!r
10031
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 50 ▶ defined $content or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $content or return 0 50 50
177 50 ▶ defined $layout or return $content;
Branch: 1 branch
BranchTrueFalseTotal
defined $layout or return $content 39 11 50
178  
179 11 $self->execute_hook(
180 'engine.template.before_layout_render',
181 $tokens, \$content
182 );
183  
184 11 my $full_content = $self->render_layout( $layout, $tokens, $content );
185  
186 11 $self->execute_hook( 'engine.template.after_layout_render',
187 \$full_content );
188  
189 # make sure to avoid ( undef ) in list context return
190 11 ▶ defined $full_content and return $full_content;
Branch: 1 branch
BranchTrueFalseTotal
defined $full_content and return $full_content 11 0 11
191 0 return;
192 }
193  
194 sub _prepare_tokens_options {
195 109 ▶ my ( $self, $tokens ) = @_;
Subroutine _prepare_tokens_options: called CC=4 SCAR=13.9
196  
197 # these are the default tokens provided for template processing
198 109 ▶ $tokens ||= {};
Condition50%1/2
$tokens ||= {}
l!l
1090
MC/DC0%0/1
$tokens ||= {}
$tokens
Truth table50%1/2
A
A= $tokens
Aresult
11
00
199 109 $tokens->{perl_version} = $^V;
200 109 $tokens->{dancer_version} = Dancer2->VERSION;
201 109 $tokens->{settings} = $self->settings;
202  
203 # no request when template is called as a global keyword
204 109 ▶ if ( $self->has_request ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_request) 103 6 109
205 103 $tokens->{request} = $self->request;
206 103 $tokens->{params} = $self->request->params;
207 103 $tokens->{vars} = $self->request->vars;
208  
209 # a session can not exist if there is no request
210 103 ▶ $tokens->{session} = $self->session->data
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_session 14 89 103
211 if $self->has_session;
212 }
213  
214 109 return $tokens;
215 }
216  
217 sub process {
218 60 ▶ my ( $self, $view, $tokens, $options ) = @_;
Subroutine process: called CC=6 SCAR=17.9
Pod documented
219 60 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 60 ▶ $tokens ||= {};
Condition100%2/2
$tokens ||= {}
l!l
3327
MC/DC100%1/1
$tokens ||= {}
$tokens
Truth table100%2/2
A
A= $tokens
Aresult
11
00
224 60 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
1248
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 60 ▶ : delete $options->{content};
Branch: 1 branch
BranchTrueFalseTotal
$view ? : 59 1 60
232  
233 51 ▶ defined $content
Branch: 1 branch
BranchTrueFalseTotal
defined $content and $full_content = $self->apply_layout($content, $tokens, $options) 50 1 51
234 and $full_content = $self->apply_layout( $content, $tokens, $options );
235  
236 51 ▶ defined $full_content
Branch: 1 branch
BranchTrueFalseTotal
defined $full_content and return $full_content 50 1 51
237 and return $full_content;
238  
239 1 croak "Template did not produce any content";
240 }
241  
242 146 1;
243  
244 __END__
« blib/lib/Dancer2/Serializer/Mutable.pm ↑ Summary blib/lib/Dancer2/ConfigUtils.pm »