1Dancer2::Template::TempUlsaetreTCooonltkriitb(u3t)ed PerDlanDcoecru2m:e:nTteamtpiloante::TemplateToolkit(3)
2
3
4
6 Dancer2::Template::TemplateToolkit - Template toolkit engine for
7 Dancer2
8
10 version 0.301004
11
13 To use this engine, you may configure Dancer2 via "config.yaml":
14
15 template: "template_toolkit"
16
17 Or you may also change the rendering engine on a per-route basis by
18 setting it manually with "set":
19
20 # code code code
21 set template => 'template_toolkit';
22
23 Most configuration variables available when creating a new instance of
24 a Template::Toolkit object can be declared inside the template toolkit
25 section on the engines configuration in your config.yml file. For
26 example:
27
28 engines:
29 template:
30 template_toolkit:
31 start_tag: '<%'
32 end_tag: '%>'
33
34 (Note: "start_tag" and "end_tag" are regexes. If you want to use PHP-
35 style tags, you will need to list them as "<\?" and "\?>".) See
36 Template::Manual::Config for the configuration variables.
37
38 In addition to the standard configuration variables, the option
39 "show_private_variables" is also available. Template::Toolkit, by
40 default, does not render private variables (the ones starting with an
41 underscore). If in your project it gets easier to disable this feature
42 than changing variable names, add this option to your configuration.
43
44 show_private_variables: true
45
46 Warning: Given the way Template::Toolkit implements this option,
47 different Dancer2 applications running within the same interpreter will
48 share this option!
49
51 This template engine allows you to use Template::Toolkit in Dancer2.
52
54 render($template, \%tokens)
55 Renders the template. The first arg is a filename for the template
56 file or a reference to a string that contains the template. The second
57 arg is a hashref for the tokens that you wish to pass to
58 Template::Toolkit for rendering.
59
61 Template::Toolkit allows you to replace certain parts, like the
62 internal STASH (Template::Stash). In order to do that, one usually
63 passes an object of another implementation such as
64 Template::Stash::AutoEscaping into the constructor.
65
66 Unfortunately that is not possible when you configure Template::Toolkit
67 from your Dancer2 configuration file. You cannot instantiate a Perl
68 object in a yaml file. Instead, you need to subclass this module, and
69 use the subclass in your configuration file.
70
71 A subclass to use the aforementioned Template::Stash::AutoEscaping
72 might look like this:
73
74 package Dancer2::Template::TemplateToolkit::AutoEscaping;
75 # or MyApp::
76
77 use Moo;
78 use Template::Stash::AutoEscaping;
79
80 extends 'Dancer2::Template::TemplateToolkit';
81
82 around '_build_engine' => sub {
83 my $orig = shift;
84 my $self = shift;
85
86 my $tt = $self->$orig(@_);
87
88 # replace the stash object
89 $tt->service->context->{STASH} = Template::Stash::AutoEscaping->new(
90 $self->config->{STASH}
91 );
92
93 return $tt;
94 };
95
96 1;
97
98 You can then use this new subclass in your config file instead of
99 "template_toolkit".
100
101 # in config.yml
102 engines:
103 template:
104 TemplateToolkit::AutoEscaping:
105 start_tag: '<%'
106 end_tag: '%>'
107 # optional arguments here
108 STASH:
109
110 The same approach should work for SERVICE (Template::Service), CONTEXT
111 (Template::Context), PARSER (Template::Parser) and GRAMMAR
112 (Template::Grammar). If you intend to replace several of these
113 components in your app, it is suggested to create an app-specific
114 subclass that handles all of them at the same time.
115
116 Template Caching
117 Template::Tookit templates can be cached by adding the "COMPILE_EXT"
118 property to your template configuration settings:
119
120 # in config.yml
121 engines:
122 template:
123 template_toolkit:
124 start_tag: '<%'
125 end_tag: '%>'
126 COMPILE_EXT: '.tcc' # cached file extension
127
128 Template caching will avoid the need to re-parse template files or
129 blocks each time they are used. Cached templates are automatically
130 updated when you update the original template file.
131
132 By default, cached templates are saved in the same directory as your
133 template. To save cached templates in a different directory, you can
134 set the "COMPILE_DIR" property in your Dancer2 configuration file.
135
136 Please see "Caching_and_Compiling_Options" in Template::Manual::Config
137 for further details and more caching options.
138
140 Dancer2, Dancer2::Core::Role::Template, Template::Toolkit.
141
143 Dancer Core Developers
144
146 This software is copyright (c) 2021 by Alexis Sukrieh.
147
148 This is free software; you can redistribute it and/or modify it under
149 the same terms as the Perl 5 programming language system itself.
150
151
152
153perl v5.34.0 2022-01-D2a1ncer2::Template::TemplateToolkit(3)