Debian Codegraph @

lib/Dancer2/Core/Role/Template.pm

raw
1 # ABSTRACT: Role for template engines
2
3 package Dancer2::Core::Role::Template;
4 $Dancer2::Core::Role::Template::VERSION = '2.2.2';
5 use Dancer2::Core::Types;
6 use Path::Tiny ();
7 use Carp 'croak';
8 use Ref::Util qw< is_ref >;
9 use Scalar::Util qw< blessed >;
10
11 use Moo::Role;
12 with 'Dancer2::Core::Role::Engine';
13
14 sub hook_aliases {
15 {
16 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 sub supported_hooks { values %{ shift->hook_aliases } }
24
25 sub _build_type {'Template'}
26
27 requires 'render';
28
29 has log_cb => (
30 is => 'ro',
31 isa => CodeRef,
32 default => sub { sub {1} },
33 );
34
35 has name => (
36 is => 'ro',
37 lazy => 1,
38 builder => 1,
39 );
40
41 sub _build_name {
42 ( my $name = ref shift ) =~ s/^Dancer2::Template:://;
43 $name;
44 }
45
46 has charset => (
47 is => 'ro',
48 isa => Str,
49 default => sub {'UTF-8'},
50 );
51
52 has default_tmpl_ext => (
53 is => 'ro',
54 isa => Str,
55 default => sub { shift->config->{extension} || 'tt' },
56 );
57
58 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 default => sub { +{} },
70 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 trigger => sub { $_[0]->_clear_views_path },
81 );
82
83 has layout => (
84 is => 'rw',
85 isa => Maybe [Str],
86 );
87
88 has layout_dir => (
89 is => 'rw',
90 isa => Maybe [Str],
91 );
92
93 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 my $self = shift;
103 return Path::Tiny::path( $self->views );
104 }
105
106 sub _template_name {
107 my ( $self, $view ) = @_;
108 my $def_tmpl_ext = $self->default_tmpl_ext();
109 $view .= ".$def_tmpl_ext" if $view !~ /\.\Q$def_tmpl_ext\E$/;
110 return $view;
111 }
112
113 sub view_pathname {
114 my ( $self, $view ) = @_;
115
116 $view = $self->_template_name($view);
117 return $self->_views_path->child($view)->stringify;
118 }
119
120 sub layout_pathname {
121 my ( $self, $layout ) = @_;
122
123 my @parts = defined $self->layout_dir ? ( $self->layout_dir ) : ();
124 return $self->_views_path->child(
125 @parts,
126 $self->_template_name($layout),
127 )->stringify;
128 }
129
130 sub pathname_exists {
131 my ( $self, $pathname ) = @_;
132 return $pathname->is_file if blessed($pathname);
133 return Path::Tiny::path($pathname)->is_file;
134 }
135
136 sub render_layout {
137 my ( $self, $layout, $tokens, $content ) = @_;
138
139 $layout = $self->layout_pathname($layout);
140
141 # FIXME: not sure if I can "just call render"
142 $self->render( $layout, { %$tokens, content => $content } );
143 }
144
145 sub apply_renderer {
146 my ( $self, $view, $tokens ) = @_;
147 $view = $self->view_pathname($view) if !is_ref($view);
148 $tokens = $self->_prepare_tokens_options( $tokens );
149
150 $self->execute_hook( 'engine.template.before_render', $tokens );
151
152 my $content = $self->render( $view, $tokens );
153 $self->execute_hook( 'engine.template.after_render', \$content );
154
155 # make sure to avoid ( undef ) in list context return
156 defined $content and return $content;
157 return;
158 }
159
160 sub apply_layout {
161 my ( $self, $content, $tokens, $options ) = @_;
162
163 $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 : ( $self->layout || $self->config->{layout} );
173
174 # that should only be $self->config, but the layout ain't there ???
175
176 defined $content or return;
177 defined $layout or return $content;
178
179 $self->execute_hook(
180 'engine.template.before_layout_render',
181 $tokens, \$content
182 );
183
184 my $full_content = $self->render_layout( $layout, $tokens, $content );
185
186 $self->execute_hook( 'engine.template.after_layout_render',
187 \$full_content );
188
189 # make sure to avoid ( undef ) in list context return
190 defined $full_content and return $full_content;
191 return;
192 }
193
194 sub _prepare_tokens_options {
195 my ( $self, $tokens ) = @_;
196
197 # these are the default tokens provided for template processing
198 $tokens ||= {};
199 $tokens->{perl_version} = $^V;
200 $tokens->{dancer_version} = Dancer2->VERSION;
201 $tokens->{settings} = $self->settings;
202
203 # no request when template is called as a global keyword
204 if ( $self->has_request ) {
205 $tokens->{request} = $self->request;
206 $tokens->{params} = $self->request->params;
207 $tokens->{vars} = $self->request->vars;
208
209 # a session can not exist if there is no request
210 $tokens->{session} = $self->session->data
211 if $self->has_session;
212 }
213
214 return $tokens;
215 }
216
217 sub process {
218 my ( $self, $view, $tokens, $options ) = @_;
219 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 $tokens ||= {};
224 $options ||= {};
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 : delete $options->{content};
232
233 defined $content
234 and $full_content = $self->apply_layout( $content, $tokens, $options );
235
236 defined $full_content
237 and return $full_content;
238
239 croak "Template did not produce any content";
240 }
241
242 1;
243
244 __END__
245
246 =pod
247
248 =encoding UTF-8
249
250 =head1 NAME
251
252 Dancer2::Core::Role::Template - Role for template engines
253
254 =head1 VERSION
255
256 version 2.2.2
257
258 =head1 DESCRIPTION
259
260 Any class that consumes this role will be able to be used as a template engine
261 under Dancer2.
262
263 In order to implement this role, the consumer B<must> implement the method C<render>. This method will receive three arguments:
264
265 =over 4
266
267 =item $self
268
269 =item $template
270
271 =item $tokens
272
273 =back
274
275 Any template receives the following tokens, by default:
276
277 =over 4
278
279 =item * C<perl_version>
280
281 Current version of perl, effectively C<$^V>.
282
283 =item * C<dancer_version>
284
285 Current version of Dancer2, effectively C<< Dancer2->VERSION >>.
286
287 =item * C<settings>
288
289 A hash of the application configuration.
290
291 =item * C<request>
292
293 The current request object.
294
295 =item * C<params>
296
297 A hash reference of all the parameters.
298
299 Currently the equivalent of C<< $request->params >>.
300
301 =item * C<vars>
302
303 The list of request variables, which is what you would get if you
304 called the C<vars> keyword.
305
306 =item * C<session>
307
308 The current session data, if a session exists.
309
310 =back
311
312 =head1 ATTRIBUTES
313
314 =head2 name
315
316 The name of the template engine (e.g.: Simple).
317
318 =head2 charset
319
320 The charset used by the template engine. This is typically set from the
321 application's C<charset> setting.
322
323 =head2 default_tmpl_ext
324
325 The default file extension. If not provided, B<tt> is used.
326
327 =head2 views
328
329 Path to the directory containing the views.
330
331 =head2 layout
332
333 Path to the directory containing the layouts.
334
335 =head2 layout_dir
336
337 Relative path to the layout directory.
338
339 Default: B<layouts>.
340
341 =head2 engine
342
343 Contains the engine.
344
345 =head1 METHODS
346
347 =head2 view_pathname($view)
348
349 Returns the full path to the requested view.
350
351 =head2 layout_pathname($layout)
352
353 Returns the full path to the requested layout.
354
355 =head2 pathname_exists($pathname)
356
357 Returns true if the requested pathname exists. Can be used for either views
358 or layouts:
359
360 $self->pathname_exists( $self->view_pathname( 'some_view' ) );
361 $self->pathname_exists( $self->layout_pathname( 'some_layout' ) );
362
363 =head2 render_layout($layout, \%tokens, \$content)
364
365 Render the layout with the applied tokens
366
367 =head2 apply_renderer($view, \%tokens)
368
369 =head2 apply_layout($content, \%tokens, \%options)
370
371 =head2 process($view, \%tokens, \%options)
372
373 =head2 template($view, \%tokens, \%options)
374
375 =head1 AUTHOR
376
377 Dancer Core Developers
378
379 =head1 COPYRIGHT AND LICENSE
380
381 This software is copyright (c) 2026 by Alexis Sukrieh.
382
383 This is free software; you can redistribute it and/or modify it under
384 the same terms as the Perl 5 programming language system itself.
385
386 =cut