1Mojolicious::Renderer(3U)ser Contributed Perl DocumentatiMoonjolicious::Renderer(3)
2
3
4
6 Mojolicious::Renderer - Generate dynamic content
7
9 use Mojolicious::Renderer;
10
11 my $renderer = Mojolicious::Renderer->new;
12 push @{$renderer->classes}, 'MyApp::Controller::Foo';
13 push @{$renderer->paths}, '/home/sri/templates';
14
16 Mojolicious::Renderer is the standard Mojolicious renderer.
17
18 See Mojolicious::Guides::Rendering for more.
19
21 Mojolicious::Renderer implements the following attributes.
22
23 cache
24 my $cache = $renderer->cache;
25 $renderer = $renderer->cache(Mojo::Cache->new);
26
27 Renderer cache, defaults to a Mojo::Cache object.
28
29 classes
30 my $classes = $renderer->classes;
31 $renderer = $renderer->classes(['main']);
32
33 Classes to use for finding templates in "DATA" sections with
34 Mojo::Loader, first one has the highest precedence, defaults to "main".
35 Only files with exactly two extensions will be used, like
36 "index.html.ep". Note that for templates to be detected, these classes
37 need to have already been loaded and added before "warmup" is called,
38 which usually happens automatically during application startup.
39
40 # Add another class with templates in DATA section
41 push @{$renderer->classes}, 'Mojolicious::Plugin::Fun';
42
43 # Add another class with templates in DATA section and higher precedence
44 unshift @{$renderer->classes}, 'Mojolicious::Plugin::MoreFun';
45
46 compress
47 my $bool = $renderer->compress;
48 $renderer = $renderer->compress($bool);
49
50 Try to negotiate compression for dynamically generated response content
51 and "gzip" compress it automatically, defaults to false. Note that this
52 attribute is EXPERIMENTAL and might change without warning!
53
54 default_format
55 my $default = $renderer->default_format;
56 $renderer = $renderer->default_format('html');
57
58 The default format to render if "format" is not set in the stash,
59 defaults to "html". Note that changing the default away from "html" is
60 not recommended, as it has the potential to break, for example, plugins
61 with bundled templates.
62
63 default_handler
64 my $default = $renderer->default_handler;
65 $renderer = $renderer->default_handler('ep');
66
67 The default template handler to use for rendering in cases where auto-
68 detection doesn't work, like for "inline" templates.
69
70 encoding
71 my $encoding = $renderer->encoding;
72 $renderer = $renderer->encoding('koi8-r');
73
74 Will encode generated content if set, defaults to "UTF-8". Note that
75 many renderers such as Mojolicious::Plugin::EPRenderer also use this
76 value to determine if template files should be decoded before
77 processing.
78
79 handlers
80 my $handlers = $renderer->handlers;
81 $renderer = $renderer->handlers({epl => sub {...}});
82
83 Registered handlers.
84
85 helpers
86 my $helpers = $renderer->helpers;
87 $renderer = $renderer->helpers({url_for => sub {...}});
88
89 Registered helpers.
90
91 paths
92 my $paths = $renderer->paths;
93 $renderer = $renderer->paths(['/home/sri/templates']);
94
95 Directories to look for templates in, first one has the highest
96 precedence.
97
98 # Add another "templates" directory
99 push @{$renderer->paths}, '/home/sri/templates';
100
101 # Add another "templates" directory with higher precedence
102 unshift @{$renderer->paths}, '/home/sri/themes/blue/templates';
103
105 Mojolicious::Renderer inherits all methods from Mojo::Base and
106 implements the following new ones.
107
108 accepts
109 my $all = $renderer->accepts(Mojolicious::Controller->new);
110 my $best = $renderer->accepts(Mojolicious::Controller->new, 'html', 'json');
111
112 Select best possible representation for Mojolicious::Controller object
113 from "format" "GET"/"POST" parameter, "format" stash value, or "Accept"
114 request header, defaults to returning the first extension if no
115 preference could be detected.
116
117 add_handler
118 $renderer = $renderer->add_handler(epl => sub {...});
119
120 Register a handler.
121
122 $renderer->add_handler(foo => sub {
123 my ($renderer, $c, $output, $options) = @_;
124 ...
125 $$output = 'Hello World!';
126 });
127
128 add_helper
129 $renderer = $renderer->add_helper(url_for => sub {...});
130
131 Register a helper.
132
133 $renderer->add_helper(foo => sub {
134 my ($c, @args) = @_;
135 ...
136 });
137
138 get_data_template
139 my $template = $renderer->get_data_template({
140 template => 'foo/bar',
141 format => 'html',
142 handler => 'epl'
143 });
144
145 Return a "DATA" section template from "classes" for an options hash
146 reference with "template", "format", "variant" and "handler" values, or
147 "undef" if no template could be found, usually used by handlers.
148
149 get_helper
150 my $helper = $renderer->get_helper('url_for');
151
152 Get a helper by full name, generate a helper dynamically for a prefix,
153 or return "undef" if no helper or prefix could be found. Generated
154 helpers return a proxy object containing the current controller object
155 and on which nested helpers can be called.
156
157 render
158 my ($output, $format) = $renderer->render(Mojolicious::Controller->new, {
159 template => 'foo/bar',
160 foo => 'bar'
161 });
162
163 Render output through one of the renderers. See "render" in
164 Mojolicious::Controller for a more user-friendly interface.
165
166 respond
167 my $bool = $renderer->respond(Mojolicious::Controller->new, $output, $format);
168 my $bool = $renderer->respond(
169 Mojolicious::Controller->new, $output, $format, $status);
170
171 Finalize dynamically generated response content and "compress" it if
172 possible. Note that this method is EXPERIMENTAL and might change
173 without warning!
174
175 template_for
176 my $name = $renderer->template_for(Mojolicious::Controller->new);
177
178 Return default template name for Mojolicious::Controller object, or
179 "undef" if no name could be generated.
180
181 template_handler
182 my $handler = $renderer->template_handler({
183 template => 'foo/bar',
184 format => 'html'
185 });
186
187 Return handler for an options hash reference with "template", "format"
188 and "variant" values, or "undef" if no handler could be found.
189
190 template_name
191 my $template = $renderer->template_name({
192 template => 'foo/bar',
193 format => 'html',
194 handler => 'epl'
195 });
196
197 Return a template name for an options hash reference with "template",
198 "format", "variant" and "handler" values, or "undef" if no template
199 could be found, usually used by handlers.
200
201 template_path
202 my $path = $renderer->template_path({
203 template => 'foo/bar',
204 format => 'html',
205 handler => 'epl'
206 });
207
208 Return the full template path for an options hash reference with
209 "template", "format", "variant" and "handler" values, or "undef" if the
210 file does not exist in "paths", usually used by handlers.
211
212 warmup
213 $renderer->warmup;
214
215 Prepare templates from "classes" for future use.
216
218 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
219
220
221
222perl v5.28.0 2018-11-01 Mojolicious::Renderer(3)