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. 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   min_compress_size
92         my $size  = $renderer->min_compress_size;
93         $renderer = $renderer->min_compress_size(1024);
94
95       Minimum output size in bytes required for compression to be used if
96       enabled, defaults to 860. Note that this attribute is EXPERIMENTAL and
97       might change without warning!
98
99   paths
100         my $paths = $renderer->paths;
101         $renderer = $renderer->paths(['/home/sri/templates']);
102
103       Directories to look for templates in, first one has the highest
104       precedence.
105
106         # Add another "templates" directory
107         push @{$renderer->paths}, '/home/sri/templates';
108
109         # Add another "templates" directory with higher precedence
110         unshift @{$renderer->paths}, '/home/sri/themes/blue/templates';
111

METHODS

113       Mojolicious::Renderer inherits all methods from Mojo::Base and
114       implements the following new ones.
115
116   accepts
117         my $all  = $renderer->accepts(Mojolicious::Controller->new);
118         my $best = $renderer->accepts(Mojolicious::Controller->new, 'html', 'json');
119
120       Select best possible representation for Mojolicious::Controller object
121       from "format" "GET"/"POST" parameter, "format" stash value, or "Accept"
122       request header, defaults to returning the first extension if no
123       preference could be detected.
124
125   add_handler
126         $renderer = $renderer->add_handler(epl => sub {...});
127
128       Register a handler.
129
130         $renderer->add_handler(foo => sub {
131           my ($renderer, $c, $output, $options) = @_;
132           ...
133           $$output = 'Hello World!';
134         });
135
136   add_helper
137         $renderer = $renderer->add_helper(url_for => sub {...});
138
139       Register a helper.
140
141         $renderer->add_helper(foo => sub {
142           my ($c, @args) = @_;
143           ...
144         });
145
146   get_data_template
147         my $template = $renderer->get_data_template({
148           template       => 'foo/bar',
149           format         => 'html',
150           handler        => 'epl'
151         });
152
153       Return a "DATA" section template from "classes" for an options hash
154       reference with "template", "format", "variant" and "handler" values, or
155       "undef" if no template could be found, usually used by handlers.
156
157   get_helper
158         my $helper = $renderer->get_helper('url_for');
159
160       Get a helper by full name, generate a helper dynamically for a prefix,
161       or return "undef" if no helper or prefix could be found. Generated
162       helpers return a proxy object containing the current controller object
163       and on which nested helpers can be called.
164
165   render
166         my ($output, $format) = $renderer->render(Mojolicious::Controller->new, {
167           template => 'foo/bar',
168           foo      => 'bar'
169         });
170
171       Render output through one of the renderers. See "render" in
172       Mojolicious::Controller for a more user-friendly interface.
173
174   respond
175         my $bool = $renderer->respond(Mojolicious::Controller->new, $output, $format);
176         my $bool = $renderer->respond(
177           Mojolicious::Controller->new, $output, $format, $status);
178
179       Finalize dynamically generated response content and "compress" it if
180       possible. Note that this method is EXPERIMENTAL and might change
181       without warning!
182
183   template_for
184         my $name = $renderer->template_for(Mojolicious::Controller->new);
185
186       Return default template name for Mojolicious::Controller object, or
187       "undef" if no name could be generated.
188
189   template_handler
190         my $handler = $renderer->template_handler({
191           template => 'foo/bar',
192           format   => 'html'
193         });
194
195       Return handler for an options hash reference with "template", "format"
196       and "variant" values, or "undef" if no handler could be found.
197
198   template_name
199         my $template = $renderer->template_name({
200           template => 'foo/bar',
201           format   => 'html',
202           handler  => 'epl'
203         });
204
205       Return a template name for an options hash reference with "template",
206       "format", "variant" and "handler" values, or "undef" if no template
207       could be found, usually used by handlers.
208
209   template_path
210         my $path = $renderer->template_path({
211           template => 'foo/bar',
212           format   => 'html',
213           handler  => 'epl'
214         });
215
216       Return the full template path for an options hash reference with
217       "template", "format", "variant" and "handler" values, or "undef" if the
218       file does not exist in "paths", usually used by handlers.
219
220   warmup
221         $renderer->warmup;
222
223       Prepare templates from "classes" for future use.
224

SEE ALSO

226       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
227
228
229
230perl v5.30.1                      2020-01-30          Mojolicious::Renderer(3)
Impressum