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 false.
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 {
129           my ($renderer, $c, $output, $options) = @_;
130           ...
131           $$output = 'Hello World!';
132         });
133
134   add_helper
135         $renderer = $renderer->add_helper(url_for => sub {...});
136
137       Register a helper.
138
139         $renderer->add_helper(foo => sub {
140           my ($c, @args) = @_;
141           ...
142         });
143
144   get_data_template
145         my $template = $renderer->get_data_template({
146           template       => 'foo/bar',
147           format         => 'html',
148           handler        => 'epl'
149         });
150
151       Return a "DATA" section template from "classes" for an options hash
152       reference with "template", "format", "variant" and "handler" values, or
153       "undef" if no template could be found, usually used by handlers.
154
155   get_helper
156         my $helper = $renderer->get_helper('url_for');
157
158       Get a helper by full name, generate a helper dynamically for a prefix,
159       or return "undef" if no helper or prefix could be found. Generated
160       helpers return a proxy object containing the current controller object
161       and on which nested helpers can be called.
162
163   render
164         my ($output, $format) = $renderer->render(Mojolicious::Controller->new);
165
166       Render output through one of the renderers. See "render" in
167       Mojolicious::Controller for a more user-friendly interface.
168
169   respond
170         my $bool = $renderer->respond(Mojolicious::Controller->new, $output, $format);
171         my $bool = $renderer->respond(
172           Mojolicious::Controller->new, $output, $format, $status);
173
174       Finalize dynamically generated response content and "compress" it if
175       possible.
176
177   template_for
178         my $name = $renderer->template_for(Mojolicious::Controller->new);
179
180       Return default template name for Mojolicious::Controller object, or
181       "undef" if no name could be generated.
182
183   template_handler
184         my $handler = $renderer->template_handler({
185           template => 'foo/bar',
186           format   => 'html'
187         });
188
189       Return handler for an options hash reference with "template", "format"
190       and "variant" values, or "undef" if no handler could be found.
191
192   template_name
193         my $template = $renderer->template_name({
194           template => 'foo/bar',
195           format   => 'html',
196           handler  => 'epl'
197         });
198
199       Return a template name for an options hash reference with "template",
200       "format", "variant" and "handler" values, or "undef" if no template
201       could be found, usually used by handlers.
202
203   template_path
204         my $path = $renderer->template_path({
205           template => 'foo/bar',
206           format   => 'html',
207           handler  => 'epl'
208         });
209
210       Return the full template path for an options hash reference with
211       "template", "format", "variant" and "handler" values, or "undef" if the
212       file does not exist in "paths", usually used by handlers.
213
214   warmup
215         $renderer->warmup;
216
217       Prepare templates from "classes" for future use.
218

SEE ALSO

220       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
221
222
223
224perl v5.32.0                      2020-07-28          Mojolicious::Renderer(3)
Impressum