1Mojolicious::Renderer(3U)ser Contributed Perl DocumentatiMoonjolicious::Renderer(3)
2
3
4

NAME

6       Mojolicious::Renderer - Generate dynamic content
7

SYNOPSIS

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

DESCRIPTION

16       Mojolicious::Renderer is the standard Mojolicious renderer.
17
18       See Mojolicious::Guides::Rendering for more.
19

ATTRIBUTES

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 true.
52
53   default_format
54         my $default = $renderer->default_format;
55         $renderer   = $renderer->default_format('html');
56
57       The default format to render if "format" is not set in the stash,
58       defaults to "html". Note that changing the default away from "html" is
59       not recommended, as it has the potential to break, for example, plugins
60       with bundled templates.
61
62   default_handler
63         my $default = $renderer->default_handler;
64         $renderer   = $renderer->default_handler('ep');
65
66       The default template handler to use for rendering in cases where auto-
67       detection doesn't work, like for "inline" templates.
68
69   encoding
70         my $encoding = $renderer->encoding;
71         $renderer    = $renderer->encoding('koi8-r');
72
73       Will encode generated content if set, defaults to "UTF-8". Note that
74       many renderers such as Mojolicious::Plugin::EPRenderer also use this
75       value to determine if template files should be decoded before
76       processing.
77
78   handlers
79         my $handlers = $renderer->handlers;
80         $renderer    = $renderer->handlers({epl => sub {...}});
81
82       Registered handlers.
83
84   helpers
85         my $helpers = $renderer->helpers;
86         $renderer   = $renderer->helpers({url_for => sub {...}});
87
88       Registered helpers.
89
90   min_compress_size
91         my $size  = $renderer->min_compress_size;
92         $renderer = $renderer->min_compress_size(1024);
93
94       Minimum output size in bytes required for compression to be used if
95       enabled, defaults to 860.
96
97   paths
98         my $paths = $renderer->paths;
99         $renderer = $renderer->paths(['/home/sri/templates']);
100
101       Directories to look for templates in, first one has the highest
102       precedence.
103
104         # Add another "templates" directory
105         push @{$renderer->paths}, '/home/sri/templates';
106
107         # Add another "templates" directory with higher precedence
108         unshift @{$renderer->paths}, '/home/sri/themes/blue/templates';
109

METHODS

111       Mojolicious::Renderer inherits all methods from Mojo::Base and
112       implements the following new ones.
113
114   accepts
115         my $all  = $renderer->accepts(Mojolicious::Controller->new);
116         my $best = $renderer->accepts(Mojolicious::Controller->new, 'html', 'json');
117
118       Select best possible representation for Mojolicious::Controller object
119       from "format" "GET"/"POST" parameter, "format" stash value, or "Accept"
120       request header, defaults to returning the first extension if no
121       preference could be detected.
122
123   add_handler
124         $renderer = $renderer->add_handler(epl => sub {...});
125
126       Register a handler.
127
128         $renderer->add_handler(foo => sub ($renderer, $c, $output, $options) {
129           ...
130           $$output = 'Hello World!';
131         });
132
133   add_helper
134         $renderer = $renderer->add_helper(url_for => sub {...});
135
136       Register a helper.
137
138         $renderer->add_helper(foo => sub ($c, @args) {
139           ...
140         });
141
142   get_data_template
143         my $template = $renderer->get_data_template({
144           template       => 'foo/bar',
145           format         => 'html',
146           handler        => 'epl'
147         });
148
149       Return a "DATA" section template from "classes" for an options hash
150       reference with "template", "format", "variant" and "handler" values, or
151       "undef" if no template could be found, usually used by handlers.
152
153   get_helper
154         my $helper = $renderer->get_helper('url_for');
155
156       Get a helper by full name, generate a helper dynamically for a prefix,
157       or return "undef" if no helper or prefix could be found. Generated
158       helpers return a proxy object containing the current controller object
159       and on which nested helpers can be called.
160
161   render
162         my ($output, $format) = $renderer->render(Mojolicious::Controller->new);
163
164       Render output through one of the renderers. See "render" in
165       Mojolicious::Controller for a more user-friendly interface.
166
167   respond
168         my $bool = $renderer->respond(Mojolicious::Controller->new, $output, $format);
169         my $bool = $renderer->respond(
170           Mojolicious::Controller->new, $output, $format, $status);
171
172       Finalize dynamically generated response content and "compress" it if
173       possible.
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

SEE ALSO

218       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
219
220
221
222perl v5.36.0                      2023-01-20          Mojolicious::Renderer(3)
Impressum