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