File Coverage

blib/lib/Dancer2/Core/Role/Template.pm
Criterion Covered Total %
statement 68 72 94.4
branch 19 26 73.0
condition 7 9 77.7
subroutine 17 18 94.4
pod 7 9 77.7
total 118 134 88.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Role for template engines
2              
3             package Dancer2::Core::Role::Template;
4             $Dancer2::Core::Role::Template::VERSION = '0.400001';
5 116     116   77632 use Dancer2::Core::Types;
  116         364  
  116         892  
6 116     116   1463983 use Dancer2::FileUtils 'path';
  116         331  
  116         6481  
7 116     116   824 use Carp 'croak';
  116         330  
  116         5833  
8 116     116   1355 use Ref::Util qw< is_ref >;
  116         940  
  116         6370  
9              
10 116     116   864 use Moo::Role;
  116         363  
  116         1090  
11             with 'Dancer2::Core::Role::Engine';
12              
13             sub hook_aliases {
14             {
15 106     106 0 879 before_template_render => 'engine.template.before_render',
16             after_template_render => 'engine.template.after_render',
17             before_layout_render => 'engine.template.before_layout_render',
18             after_layout_render => 'engine.template.after_layout_render',
19             }
20             }
21              
22 27     27 0 69 sub supported_hooks { values %{ shift->hook_aliases } }
  27         120  
23              
24 0     0   0 sub _build_type {'Template'}
25              
26             requires 'render';
27              
28             has log_cb => (
29             is => 'ro',
30             isa => CodeRef,
31             default => sub { sub {1} },
32             );
33              
34             has name => (
35             is => 'ro',
36             lazy => 1,
37             builder => 1,
38             );
39              
40             sub _build_name {
41 2     2   2112 ( my $name = ref shift ) =~ s/^Dancer2::Template:://;
42 2         11 $name;
43             }
44              
45             has charset => (
46             is => 'ro',
47             isa => Str,
48             default => sub {'UTF-8'},
49             );
50              
51             has default_tmpl_ext => (
52             is => 'ro',
53             isa => Str,
54             default => sub { shift->config->{extension} || 'tt' },
55             );
56              
57             has engine => (
58             is => 'ro',
59             isa => Object,
60             lazy => 1,
61             builder => 1,
62             );
63              
64             has settings => (
65             is => 'ro',
66             isa => HashRef,
67             lazy => 1,
68             default => sub { +{} },
69             writer => 'set_settings',
70             );
71              
72             # The attributes views, layout and layout_dir have triggers in
73             # Dancer2::Core::App that enable their values to be modified by
74             # the `set` keyword. As such, these are defined as read-write attrs.
75              
76             has views => (
77             is => 'rw',
78             isa => Maybe [Str],
79             );
80              
81             has layout => (
82             is => 'rw',
83             isa => Maybe [Str],
84             );
85              
86             has layout_dir => (
87             is => 'rw',
88             isa => Maybe [Str],
89             );
90              
91             sub _template_name {
92 160     160   432 my ( $self, $view ) = @_;
93 160         524 my $def_tmpl_ext = $self->default_tmpl_ext();
94 160 100       1364 $view .= ".$def_tmpl_ext" if $view !~ /\.\Q$def_tmpl_ext\E$/;
95 160         510 return $view;
96             }
97              
98             sub view_pathname {
99 134     134 1 1055 my ( $self, $view ) = @_;
100              
101 134         401 $view = $self->_template_name($view);
102 134         2450 return path( $self->views, $view );
103             }
104              
105             sub layout_pathname {
106 2     2 1 3 my ( $self, $layout ) = @_;
107              
108 2         28 return path(
109             $self->views,
110             $self->layout_dir,
111             $self->_template_name($layout),
112             );
113             }
114              
115             sub pathname_exists {
116 113     113 1 2157 my ( $self, $pathname ) = @_;
117 113         2683 return -f $pathname;
118             }
119              
120             sub render_layout {
121 6     6 1 16 my ( $self, $layout, $tokens, $content ) = @_;
122              
123 6         25 $layout = $self->layout_pathname($layout);
124              
125             # FIXME: not sure if I can "just call render"
126 6         44 $self->render( $layout, { %$tokens, content => $content } );
127             }
128              
129             sub apply_renderer {
130 36     36 1 103 my ( $self, $view, $tokens ) = @_;
131 36 100       205 $view = $self->view_pathname($view) if !is_ref($view);
132 36         171 $tokens = $self->_prepare_tokens_options( $tokens );
133              
134 36         170 $self->execute_hook( 'engine.template.before_render', $tokens );
135              
136 32         354 my $content = $self->render( $view, $tokens );
137 31         198 $self->execute_hook( 'engine.template.after_render', \$content );
138              
139             # make sure to avoid ( undef ) in list context return
140 31 50       363 defined $content and return $content;
141 0         0 return;
142             }
143              
144             sub apply_layout {
145 31     31 1 103 my ( $self, $content, $tokens, $options ) = @_;
146              
147 31         89 $tokens = $self->_prepare_tokens_options( $tokens );
148              
149             # If 'layout' was given in the options hashref, use it if it's a true value,
150             # or don't use a layout if it was false (0, or undef); if layout wasn't
151             # given in the options hashref, go with whatever the current layout setting
152             # is.
153             my $layout =
154             exists $options->{layout}
155             ? ( $options->{layout} ? $options->{layout} : undef )
156 31 50 66     873 : ( $self->layout || $self->config->{layout} );
    100          
157              
158             # that should only be $self->config, but the layout ain't there ???
159              
160 31 50       369 defined $content or return;
161 31 100       117 defined $layout or return $content;
162              
163 6         21 $self->execute_hook(
164             'engine.template.before_layout_render',
165             $tokens, \$content
166             );
167              
168 6         41 my $full_content = $self->render_layout( $layout, $tokens, $content );
169              
170 6         31 $self->execute_hook( 'engine.template.after_layout_render',
171             \$full_content );
172              
173             # make sure to avoid ( undef ) in list context return
174 6 50       46 defined $full_content and return $full_content;
175 0         0 return;
176             }
177              
178             sub _prepare_tokens_options {
179 67     67   187 my ( $self, $tokens ) = @_;
180              
181             # these are the default tokens provided for template processing
182 67   50     188 $tokens ||= {};
183 67         172 $tokens->{perl_version} = $^V;
184 67         423 $tokens->{dancer_version} = Dancer2->VERSION;
185 67         1387 $tokens->{settings} = $self->settings;
186              
187             # no request when template is called as a global keyword
188 67 100       842 if ( $self->has_request ) {
189 65         219 $tokens->{request} = $self->request;
190 65         305 $tokens->{params} = $self->request->params;
191 65         248 $tokens->{vars} = $self->request->vars;
192              
193             # a session can not exist if there is no request
194 65 100       394 $tokens->{session} = $self->session->data
195             if $self->has_session;
196             }
197              
198 67         214 return $tokens;
199             }
200              
201             sub process {
202 36     36 1 113 my ( $self, $view, $tokens, $options ) = @_;
203 36         82 my ( $content, $full_content );
204              
205             # it's important that $tokens is not undef, so that things added to it via
206             # a before_template in apply_renderer survive to the apply_layout. GH#354
207 36   100     2027 $tokens ||= {};
208 36   100     184 $options ||= {};
209              
210             ## FIXME - Look into PR 654 so we fix the problem here as well!
211              
212             $content =
213             $view
214             ? $self->apply_renderer( $view, $tokens )
215 36 50       196 : delete $options->{content};
216              
217 31 50       162 defined $content
218             and $full_content = $self->apply_layout( $content, $tokens, $options );
219              
220 31 50       183 defined $full_content
221             and return $full_content;
222              
223 0           croak "Template did not produce any content";
224             }
225              
226             1;
227              
228             __END__
229              
230             =pod
231              
232             =encoding UTF-8
233              
234             =head1 NAME
235              
236             Dancer2::Core::Role::Template - Role for template engines
237              
238             =head1 VERSION
239              
240             version 0.400001
241              
242             =head1 DESCRIPTION
243              
244             Any class that consumes this role will be able to be used as a template engine
245             under Dancer2.
246              
247             In order to implement this role, the consumer B<must> implement the method C<render>. This method will receive three arguments:
248              
249             =over 4
250              
251             =item $self
252              
253             =item $template
254              
255             =item $tokens
256              
257             =back
258              
259             Any template receives the following tokens, by default:
260              
261             =over 4
262              
263             =item * C<perl_version>
264              
265             Current version of perl, effectively C<$^V>.
266              
267             =item * C<dancer_version>
268              
269             Current version of Dancer2, effectively C<< Dancer2->VERSION >>.
270              
271             =item * C<settings>
272              
273             A hash of the application configuration.
274              
275             =item * C<request>
276              
277             The current request object.
278              
279             =item * C<params>
280              
281             A hash reference of all the parameters.
282              
283             Currently the equivalent of C<< $request->params >>.
284              
285             =item * C<vars>
286              
287             The list of request variables, which is what you would get if you
288             called the C<vars> keyword.
289              
290             =item * C<session>
291              
292             The current session data, if a session exists.
293              
294             =back
295              
296             =head1 ATTRIBUTES
297              
298             =head2 name
299              
300             The name of the template engine (e.g.: Simple).
301              
302             =head2 charset
303              
304             The charset. The default value is B<UTF-8>.
305              
306             =head2 default_tmpl_ext
307              
308             The default file extension. If not provided, B<tt> is used.
309              
310             =head2 views
311              
312             Path to the directory containing the views.
313              
314             =head2 layout
315              
316             Path to the directory containing the layouts.
317              
318             =head2 layout_dir
319              
320             Relative path to the layout directory.
321              
322             Default: B<layouts>.
323              
324             =head2 engine
325              
326             Contains the engine.
327              
328             =head1 METHODS
329              
330             =head2 view_pathname($view)
331              
332             Returns the full path to the requested view.
333              
334             =head2 layout_pathname($layout)
335              
336             Returns the full path to the requested layout.
337              
338             =head2 pathname_exists($pathname)
339              
340             Returns true if the requested pathname exists. Can be used for either views
341             or layouts:
342              
343             $self->pathname_exists( $self->view_pathname( 'some_view' ) );
344             $self->pathname_exists( $self->layout_pathname( 'some_layout' ) );
345              
346             =head2 render_layout($layout, \%tokens, \$content)
347              
348             Render the layout with the applied tokens
349              
350             =head2 apply_renderer($view, \%tokens)
351              
352             =head2 apply_layout($content, \%tokens, \%options)
353              
354             =head2 process($view, \%tokens, \%options)
355              
356             =head2 template($view, \%tokens, \%options)
357              
358             =head1 AUTHOR
359              
360             Dancer Core Developers
361              
362             =head1 COPYRIGHT AND LICENSE
363              
364             This software is copyright (c) 2023 by Alexis Sukrieh.
365              
366             This is free software; you can redistribute it and/or modify it under
367             the same terms as the Perl 5 programming language system itself.
368              
369             =cut