Mojolicious/Renderer.pm

filter: Statementstmt 90.5% 154 / 170 Branchbran 70.6% 65 / 92 Conditioncond 52.5% 31 / 59 MC/DCmcdc 34.2% 12 / 35 Subroutinesub 96.8% 31 / 32 Podpod 100% 12 / 12 totaltotal 76.2% 305 / 400 CC 78 SCAR 28.8
CC
78
Cov
78%
accepts
110.0
template_name
25.0
render
22.4
CRAP 17.8
« Mojolicious/Validator/Validation.pm ↑ Summary Mojo/DOM/CSS.pm »
1 package Mojolicious::Renderer;
2 20 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Mojo::DynamicMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Mojo::File qw(curfile path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Mojo::Loader qw(data_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Mojo::Util qw(decamelize deprecated encode gzip md5_sum monkey_patch);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶has cache => sub { Mojo::Cache->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
13 38 ▶has classes => sub { ['main'] };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 20 has compress => 1;
15 20 has default_format => 'html';
16 20 has 'default_handler';
17 20 has encoding => 'UTF-8';
18 166 ▶has [qw(handlers helpers)] => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 20 has min_compress_size => 860;
20 83 ▶has paths => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 # Bundled templates
23 20 my $TEMPLATES = curfile->sibling('resources', 'templates');
24  
25 62 ▶sub DESTROY { Mojo::Util::_teardown($_) for @{shift->{namespaces}} }
Subroutine DESTROY: called CC=2 SCAR=6.9
26  
27 0 ▶sub accepts {
Subroutine accepts: not called CC=10 SCAR=47.0
Pod documented
28 0 my ($self, $c) = (shift, shift);
29  
30 # DEPRECATED!
31 0 my $req = $c->req;
32 0 my $param = $req->param('format');
33 0 ▶ deprecated 'The ?format=* parameter is deprecated in favor of ?_format=* for content negotiation' if defined $param;
Branch: 1 branch
BranchTrueFalseTotal
if defined $param 0 0 0
34  
35 # List representations
36 0 ▶ my $fmt = $param // $req->param('_format') || $c->stash->{format};
Condition0%0/6
($param // $req->param('_format')) || $c->stash->{'format'}
l!l&&r!l&&!r
000
$param // $req->param("_format")
l!l&&r!l&&!r
000
MC/DC0%0/3
($param // $req->param('_format')) || $c->stash->{'format'}
$param $req->param("_format") $c->stash->{'format'}
Truth table0%0/4
A // B || C
A= $paramB= $req->param("_format")C= $c->stash->{'format'}
ABCresult
1XX1
01X1
0011
0000
37 0 ▶ my @exts = $fmt ? ($fmt) : ();
Branch: 1 branch
BranchTrueFalseTotal
$fmt ? : 0 0 0
38 0 push @exts, @{$c->app->types->detect($req->headers->accept)};
39 0 ▶ return \@exts unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
40  
41 # Find best representation
42 0 ▶ for my $ext (@exts) { $ext eq $_ and return $ext for @_ }
Branch: 1 branch
BranchTrueFalseTotal
$ext eq $_ and return $ext 0 0 0
43 0 ▶ return @exts ? undef : shift;
Branch: 1 branch
BranchTrueFalseTotal
@exts ? : 0 0 0
44 }
45  
46 209 ▶sub add_handler { $_[0]->handlers->{$_[1]} = $_[2] and return $_[0] }
Subroutine add_handler: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->handlers->{$_[1]} = $_[2] and return $_[0] 209 0 209
47  
48 7271 ▶sub add_helper {
Subroutine add_helper: called CC=3 SCAR=11.0
Pod documented
49 7271 my ($self, $name, $cb) = @_;
50  
51 7271 $self->helpers->{$name} = $cb;
52 7271 delete $self->{proxy};
53 7271 ▶ $cb = $self->get_helper($name) if $name =~ s/\..*$//;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ s/\..*$//u 1663 5608 7271
54 7271 Mojo::DynamicMethods::register $_, $self, $name, $cb for qw(Mojolicious Mojolicious::Controller);
55  
56 7271 return $self;
57 }
58  
59 70 ▶sub get_data_template {
Subroutine get_data_template: called CC=2 SCAR=7.1
Pod documented
60 70 my ($self, $options) = @_;
61 70 ▶ return undef unless my $template = $self->template_name($options);
Branch: 1 branch
BranchTrueFalseTotal
unless my $template = $self->template_name($options) 0 70 70
62 70 return data_section $self->{index}{$template}, $template;
63 }
64  
65 13895 ▶sub get_helper {
Subroutine get_helper: called CC=7 SCAR=19.5
Pod documented
66 13895 my ($self, $name) = @_;
67  
68 13895 ▶ if (my $h = $self->{proxy}{$name} || $self->helpers->{$name}) { return $h }
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $self->{"proxy"}{$name} || $self->helpers->{$name}) 12134 1761 13895
Condition100%3/3
$self->{'proxy'}{$name} || $self->helpers->{$name}
l!l&&r!l&&!r
1233109011761
MC/DC100%2/2
$self->{'proxy'}{$name} || $self->helpers->{$name}
$self->{'proxy'}{$name} $self->helpers->{$name}
Truth table100%3/3
A || B
A= $self->{'proxy'}{$name}B= $self->helpers->{$name}
ABresult
1X1
011
000
69  
70 1761 my $found;
71 1761 my $class = 'Mojolicious::Renderer::Helpers::' . md5_sum "$name:$self";
72 1761 ▶ my $re = length $name ? qr/^(\Q$name\E\.([^.]+))/ : qr/^(([^.]+))/;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 1738 23 1761
73 1761 for my $key (keys %{$self->helpers}) {
74 76096 ▶ $key =~ $re ? ($found, my $method) = (1, $2) : next;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /$re/u ? : 11365 64731 76096
75 11365 my $sub = $self->get_helper($1);
76 11365 ▶ monkey_patch $class, $method => sub { ${shift()}->$sub(@_) };
Subroutine exception: called CC=1 SCAR=0.0
Subroutine server_timing: called CC=1 SCAR=0.0
77 }
78  
79 1761 ▶ $found ? push @{$self->{namespaces}}, $class : return undef;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 1757 4 1761
80 1757 ▶ return $self->{proxy}{$name} = sub { bless \(my $dummy = shift), $class };
Subroutine __ANON__: called CC=1 SCAR=0.0
81 }
82  
83 222 ▶sub render {
Subroutine render: called CC=20 SCAR=31.1
Pod documented
84 222 my ($self, $c) = @_;
85  
86 222 my $stash = $c->stash;
87 my $options = {
88 encoding => $self->encoding,
89 handler => $stash->{handler},
90 template => delete $stash->{template},
91 variant => $stash->{variant}
92 222 };
93 222 my $inline = $options->{inline} = delete $stash->{inline};
94 222 ▶ $options->{handler} //= $self->default_handler if defined $inline;
Branch: 1 branch
BranchTrueFalseTotal
if defined $inline 5 217 222
Condition33%1/3
$options->{"handler"} //= $self->default_handler
l!l&&r!l&&!r
050
MC/DC0%0/2
$options->{"handler"} //= $self->default_handler
$options->{"handler"} $self->default_handler
Truth table33%1/3
A //= B
A= $options->{"handler"}B= $self->default_handler
ABresult
1X1
011
000
95 222 ▶ $options->{format} = $stash->{format} || $self->default_format;
Condition66%2/3
$stash->{'format'} || $self->default_format
l!l&&r!l&&!r
1101120
MC/DC0%0/2
$stash->{'format'} || $self->default_format
$stash->{'format'} $self->default_format
Truth table66%2/3
A || B
A= $stash->{'format'}B= $self->default_format
ABresult
1X1
011
000
96  
97 # Data
98 222 ▶ return delete $stash->{data}, $options->{format} if defined $stash->{data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $stash->{"data"} 2 220 222
99  
100 # Text
101 220 ▶ return _maybe($options->{encoding}, delete $stash->{text}), $options->{format} if defined $stash->{text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $stash->{"text"} 67 153 220
102  
103 # JSON
104 153 ▶ return encode_json(delete $stash->{json}), 'json' if exists $stash->{json};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stash->{"json"} 2 151 153
105  
106 # Template or templateless handler
107 151 ▶ $options->{template} //= $self->template_for($c);
Condition100%3/3
$options->{"template"} //= $self->template_for($c)
l!l&&r!l&&!r
124225
MC/DC100%2/2
$options->{"template"} //= $self->template_for($c)
$options->{"template"} $self->template_for($c)
Truth table100%3/3
A //= B
A= $options->{"template"}B= $self->template_for($c)
ABresult
1X1
011
000
108 151 ▶ return () unless $self->_render_template($c, \my $output, $options);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_render_template($c, \my $output, $options) 61 87 148
109  
110 # Inheritance
111 87 ▶ my $content = $stash->{'mojo.content'} //= {};
Condition100%2/2
$stash->{"mojo.content"} //= {}
l!l
1077
MC/DC100%1/1
$stash->{"mojo.content"} //= {}
$stash->{"mojo.content"}
Truth table100%2/2
A
A= $stash->{"mojo.content"}
Aresult
11
00
112 87 ▶ local $content->{content} = $output =~ /\S/ ? $output : undef if $stash->{extends} || $stash->{layout};
Branch: 2 branches
BranchTrueFalseTotal
if $stash->{"extends"} or $stash->{"layout"} 6 81 87
$output =~ /\S/u ? : 6 0 6
Condition66%2/3
$stash->{"extends"} or $stash->{"layout"}
l!l&&r!l&&!r
0681
MC/DC50%1/2
$stash->{"extends"} or $stash->{"layout"}
$stash->{"extends"} $stash->{"layout"}
Truth table66%2/3
A or B
A= $stash->{"extends"}B= $stash->{"layout"}
ABresult
1X1
011
000
113 87 ▶ while ((my $next = _next($stash)) && !defined $inline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = _next($stash) and not defined $inline) 6 87 93
Condition66%2/3
my $next = _next($stash) and not defined $inline
!ll&&!rl&&r
8706
MC/DC50%1/2
my $next = _next($stash) and not defined $inline
my $next = _next($stash) not defined $inline
Truth table66%2/3
A and not(B)
A= my $next = _next($stash)B= not defined $inline
ABresult
0X0
100
111
114 6 @$options{qw(handler template)} = ($stash->{handler}, $next);
115 6 ▶ $options->{format} = $stash->{format} || $self->default_format;
Condition33%1/3
$stash->{'format'} || $self->default_format
l!l&&r!l&&!r
060
MC/DC0%0/2
$stash->{'format'} || $self->default_format
$stash->{'format'} $self->default_format
Truth table33%1/3
A || B
A= $stash->{'format'}B= $self->default_format
ABresult
1X1
011
000
116 6 ▶ if ($self->_render_template($c, \my $tmp, $options)) { $output = $tmp }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_render_template($c, \my $tmp, $options)) 6 0 6
117 6 ▶ $content->{content} //= $output if $output =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ /\S/u 6 0 6
Condition33%1/3
$content->{"content"} //= $output
l!l&&r!l&&!r
600
MC/DC0%0/2
$content->{"content"} //= $output
$content->{"content"} $output
Truth table33%1/3
A //= B
A= $content->{"content"}B= $output
ABresult
1X1
011
000
118 }
119  
120 87 ▶ return $output if $stash->{'mojo.string'};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"mojo.string"} 8 79 87
121 79 return _maybe($options->{encoding}, $output), $options->{format};
122 }
123  
124 149 ▶sub respond {
Subroutine respond: called CC=7 SCAR=19.5
Pod documented
125 149 my ($self, $c, $output, $format, $status) = @_;
126  
127 149 ▶ croak 'A response has already been rendered' if $c->stash->{'mojo.respond'}++;
Branch: 1 branch
BranchTrueFalseTotal
if $c->stash->{"mojo.respond"}++ 1 148 149
128  
129 # Gzip compression
130 148 my $res = $c->res;
131 148 ▶ if ($self->compress && length($output) >= $self->min_compress_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->compress and length $output >= $self->min_compress_size) 21 127 148
Condition100%3/3
$self->compress and length $output >= $self->min_compress_size
!ll&&!rl&&r
112621
MC/DC100%2/2
$self->compress and length $output >= $self->min_compress_size
$self->compress length $output >= $self->min_compress_size
Truth table100%3/3
A and B
A= $self->compressB= length $output >= $self->min_compress_size
ABresult
0X0
100
111
132 21 my $headers = $res->headers;
133 21 $headers->append(Vary => 'Accept-Encoding');
134 21 ▶ my $gzip = ($c->req->headers->accept_encoding // '') =~ /gzip/i;
Condition100%2/2
$c->req->headers->accept_encoding // ""
l!l
183
MC/DC100%1/1
$c->req->headers->accept_encoding // ""
$c->req->headers->accept_encoding
Truth table100%2/2
A
A= $c->req->headers->accept_encoding
Aresult
11
00
135 21 ▶ if ($gzip && !$headers->content_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gzip and not $headers->content_encoding) 18 3 21
Condition66%2/3
$gzip and not $headers->content_encoding
!ll&&!rl&&r
3018
MC/DC50%1/2
$gzip and not $headers->content_encoding
$gzip not $headers->content_encoding
Truth table66%2/3
A and not(B)
A= $gzipB= not $headers->content_encoding
ABresult
0X0
100
111
136 18 $headers->content_encoding('gzip');
137 18 $output = gzip $output;
138 }
139 }
140  
141 148 $res->body($output);
142 148 $c->app->types->content_type($c, {ext => $format});
143 148 return !!$c->rendered($status);
144 }
145  
146 28 ▶sub template_for {
Subroutine template_for: called CC=4 SCAR=13.9
Pod documented
147 28 my ($self, $c) = @_;
148  
149 # Normal default template
150 28 my $stash = $c->stash;
151 28 my ($controller, $action) = @$stash{qw(controller action)};
152 28 ▶ return join '/', split(/-/, decamelize $controller), $action if $controller && $action;
Branch: 1 branch
BranchTrueFalseTotal
if $controller and $action 19 9 28
Condition66%2/3
$controller and $action
!ll&&!rl&&r
9019
MC/DC50%1/2
$controller and $action
$controller $action
Truth table66%2/3
A and B
A= $controllerB= $action
ABresult
0X0
100
111
153  
154 # Try the route name if we don't have controller and action
155 9 ▶ return undef unless my $route = $c->match->endpoint;
Branch: 1 branch
BranchTrueFalseTotal
unless my $route = $c->match->endpoint 5 4 9
156 4 return $route->name;
157 }
158  
159 120 ▶sub template_handler {
Subroutine template_handler: called CC=3 SCAR=11.0
Pod documented
160 120 my ($self, $options) = @_;
161 120 ▶ return undef unless my $file = $self->template_name($options);
Branch: 1 branch
BranchTrueFalseTotal
unless my $file = $self->template_name($options) 0 120 120
162 120 ▶ return $self->default_handler unless my $handlers = $self->{templates}{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless my $handlers = $self->{"templates"}{$file} 60 60 120
163 60 return $handlers->[0];
164 }
165  
166 534 ▶sub template_name {
Subroutine template_name: called CC=10 SCAR=32.2
Pod documented
167 534 my ($self, $options) = @_;
168  
169 534 ▶ return undef unless defined(my $template = $options->{template});
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $template = $options->{"template"}) 0 534 534
170 534 ▶ return undef unless my $format = $options->{format};
Branch: 1 branch
BranchTrueFalseTotal
unless my $format = $options->{"format"} 0 534 534
171 534 $template .= ".$format";
172  
173 534 ▶ $self->warmup unless $self->{templates};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"templates"} 8 526 534
174  
175 # Variants
176 534 my $handler = $options->{handler};
177 534 ▶ if (defined(my $variant = $options->{variant})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $variant = $options->{"variant"})) 0 534 534
178 0 $variant = "$template+$variant";
179 0 ▶ my $handlers = $self->{templates}{$variant} // [];
Condition0%0/2
$self->{"templates"}{$variant} // []
l!l
00
MC/DC0%0/1
$self->{"templates"}{$variant} // []
$self->{"templates"}{$variant}
Truth table0%0/2
A
A= $self->{"templates"}{$variant}
Aresult
11
00
180 0 ▶ $template = $variant if @$handlers && !defined $handler || grep { $_ eq $handler } @$handlers;
Branch: 1 branch
BranchTrueFalseTotal
if @$handlers and not defined $handler or grep {$_ eq $handler;} @$handlers 0 0 0
Condition0%0/6
@$handlers and not defined $handler or grep {$_ eq $handler;} @$handlers
l!l&&r!l&&!r
000
@$handlers and not defined $handler
!ll&&!rl&&r
000
MC/DC0%0/3
@$handlers and not defined $handler or grep {$_ eq $handler;} @$handlers
@$handlers not defined $handler grep {$_ eq $handler;} @$handlers
Truth table0%0/5
A and not(B) or C
A= @$handlersB= not defined $handlerC= grep {$_ eq $handler;} @$handlers
ABCresult
11X1
0X11
1011
0X00
1000
181 }
182  
183 534 ▶ return defined $handler ? "$template.$handler" : $template;
Branch: 1 branch
BranchTrueFalseTotal
defined $handler ? : 414 120 534
184 }
185  
186 121 ▶sub template_path {
Subroutine template_path: called CC=4 SCAR=13.9
Pod documented
187 121 my ($self, $options) = @_;
188 121 ▶ return undef unless my $name = $self->template_name($options);
Branch: 1 branch
BranchTrueFalseTotal
unless my $name = $self->template_name($options) 0 121 121
189 121 my @parts = split /\//, $name;
190 121 ▶ -r and return $_ for map { path($_, @parts)->to_string } @{$self->paths}, $TEMPLATES;
Branch: 1 branch
BranchTrueFalseTotal
-r $_ and return $_ 51 144 195
191 70 return undef;
192 }
193  
194 26 ▶sub warmup {
Subroutine warmup: called CC=8 SCAR=20.9
Pod documented
195 26 my $self = shift;
196  
197 26 my ($index, $templates) = @$self{qw(index templates)} = ({}, {});
198  
199 # Handlers for templates
200 26 for my $path (@{$self->paths}, $TEMPLATES) {
201 403 s/\.(\w+)$// and push @{$templates->{$_}}, $1
202 52 ▶ for path($path)->list_tree->map(sub { join '/', @{$_->to_rel($path)} })->each;
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
s/\.(\w+)$//u and push @{$$templates{$_};}, $1 403 0 403
203 }
204  
205 # Handlers and classes for DATA templates
206 26 for my $class (reverse @{$self->classes}) {
207 60 $index->{$_} = $class for my @keys = sort keys %{data_section $class};
208 60 ▶ s/\.(\w+)$// and unshift @{$templates->{$_}}, $1 for reverse @keys;
Branch: 1 branch
BranchTrueFalseTotal
s/\.(\w+)$//u and unshift @{$$templates{$_};}, $1 102 0 102
209 }
210 }
211  
212 146 ▶sub _maybe { $_[0] ? encode @_ : $_[1] }
Subroutine _maybe: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 146 0 146
213  
214 93 ▶sub _next {
Subroutine _next: called CC=3 SCAR=11.0
215 93 my $stash = shift;
216 93 ▶ return delete $stash->{extends} if $stash->{extends};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"extends"} 0 93 93
217 93 ▶ return undef unless my $layout = delete $stash->{layout};
Branch: 1 branch
BranchTrueFalseTotal
unless my $layout = delete $stash->{"layout"} 87 6 93
218 6 return join '/', 'layouts', $layout;
219 }
220  
221 157 ▶sub _render_template {
Subroutine _render_template: called CC=6 SCAR=18.4
222 157 my ($self, $c, $output, $options) = @_;
223  
224 157 ▶ my $handler = $options->{handler} ||= $self->template_handler($options);
Condition66%2/3
$options->{"handler"} ||= $self->template_handler($options)
l!l&&r!l&&!r
381190
MC/DC0%0/2
$options->{"handler"} ||= $self->template_handler($options)
$options->{"handler"} $self->template_handler($options)
Truth table66%2/3
A ||= B
A= $options->{"handler"}B= $self->template_handler($options)
ABresult
1X1
011
000
225 157 ▶ return undef unless $handler;
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 157 157
226 $c->helpers->log->error(qq{No handler for "$handler" found}) and return undef
227 157 ▶ unless my $renderer = $self->handlers->{$handler};
Branch: 1 branch
BranchTrueFalseTotal
unless my $renderer = $self->handlers->{$handler} 1 156 157
Condition60%3/5
(my $renderer = $self->handlers->{$handler}) || $c->helpers->log->error(qq[No handler for "$handler" found]) && (return undef)
l!l&&r!l&&!r
15610
$c->helpers->log->error(qq[No handler for "$handler" found]) and return undef
!ll
01
MC/DC0%0/2
(my $renderer = $self->handlers->{$handler}) || $c->helpers->log->error(qq[No handler for "$handler" found]) && (return undef)
(my $renderer = $self->handlers->{$handler}) $c->helpers->log->error(qq[No handler for "$handler" found])
Truth table66%2/3
A || B
A= (my $renderer = $self->handlers->{$handler})B= $c->helpers->log->error(qq[No handler for "$handler" found])
ABresult
1X1
011
000
228  
229 156 $renderer->($self, $c, $output, $options);
230 153 ▶ return 1 if defined $$output;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$output 93 60 153
231 }
232  
233 20 1;
234  
235 =encoding utf8
236  
237 =head1 NAME
238  
239 Mojolicious::Renderer - Generate dynamic content
240  
241 =head1 SYNOPSIS
242  
243 use Mojolicious::Renderer;
244  
245 my $renderer = Mojolicious::Renderer->new;
246 push @{$renderer->classes}, 'MyApp::Controller::Foo';
247 push @{$renderer->paths}, '/home/sri/templates';
248  
249 =head1 DESCRIPTION
250  
251 L<Mojolicious::Renderer> is the standard L<Mojolicious> renderer.
252  
253 See L<Mojolicious::Guides::Rendering> for more.
254  
255 =head1 ATTRIBUTES
256  
257 L<Mojolicious::Renderer> implements the following attributes.
258  
259 =head2 cache
260  
261 my $cache = $renderer->cache;
262 $renderer = $renderer->cache(Mojo::Cache->new);
263  
264 Renderer cache, defaults to a L<Mojo::Cache> object.
265  
266 =head2 classes
267  
268 my $classes = $renderer->classes;
269 $renderer = $renderer->classes(['main']);
270  
271 Classes to use for finding templates in C<DATA> sections with L<Mojo::Loader>, first one has the highest precedence,
272 defaults to C<main>. Only files with exactly two extensions will be used, like C<index.html.ep>. Note that for
273 templates to be detected, these classes need to have already been loaded and added before L</"warmup"> is called, which
274 usually happens automatically during application startup.
275  
276 # Add another class with templates in DATA section
277 push @{$renderer->classes}, 'Mojolicious::Plugin::Fun';
278  
279 # Add another class with templates in DATA section and higher precedence
280 unshift @{$renderer->classes}, 'Mojolicious::Plugin::MoreFun';
281  
282 =head2 compress
283  
284 my $bool = $renderer->compress;
285 $renderer = $renderer->compress($bool);
286  
287 Try to negotiate compression for dynamically generated response content and C<gzip> compress it automatically, defaults
288 to true.
289  
290 =head2 default_format
291  
292 my $default = $renderer->default_format;
293 $renderer = $renderer->default_format('html');
294  
295 The default format to render if C<format> is not set in the stash, defaults to C<html>. Note that changing the default
296 away from C<html> is not recommended, as it has the potential to break, for example, plugins with bundled templates.
297  
298 =head2 default_handler
299  
300 my $default = $renderer->default_handler;
301 $renderer = $renderer->default_handler('ep');
302  
303 The default template handler to use for rendering in cases where auto-detection doesn't work, like for C<inline>
304 templates.
305  
306 =head2 encoding
307  
308 my $encoding = $renderer->encoding;
309 $renderer = $renderer->encoding('koi8-r');
310  
311 Will encode generated content if set, defaults to C<UTF-8>. Note that many renderers such as
312 L<Mojolicious::Plugin::EPRenderer> also use this value to determine if template files should be decoded before
313 processing.
314  
315 =head2 handlers
316  
317 my $handlers = $renderer->handlers;
318 $renderer = $renderer->handlers({epl => sub {...}});
319  
320 Registered handlers.
321  
322 =head2 helpers
323  
324 my $helpers = $renderer->helpers;
325 $renderer = $renderer->helpers({url_for => sub {...}});
326  
327 Registered helpers.
328  
329 =head2 min_compress_size
330  
331 my $size = $renderer->min_compress_size;
332 $renderer = $renderer->min_compress_size(1024);
333  
334 Minimum output size in bytes required for compression to be used if enabled, defaults to C<860>.
335  
336 =head2 paths
337  
338 my $paths = $renderer->paths;
339 $renderer = $renderer->paths(['/home/sri/templates']);
340  
341 Directories to look for templates in, first one has the highest precedence.
342  
343 # Add another "templates" directory
344 push @{$renderer->paths}, '/home/sri/templates';
345  
346 # Add another "templates" directory with higher precedence
347 unshift @{$renderer->paths}, '/home/sri/themes/blue/templates';
348  
349 =head1 METHODS
350  
351 L<Mojolicious::Renderer> inherits all methods from L<Mojo::Base> and implements the following new ones.
352  
353 =head2 accepts
354  
355 my $all = $renderer->accepts(Mojolicious::Controller->new);
356 my $best = $renderer->accepts(Mojolicious::Controller->new, 'html', 'json');
357  
358 Select best possible representation for L<Mojolicious::Controller> object from C<format> C<GET>/C<POST> parameter,
359 C<format> stash value, or C<Accept> request header, defaults to returning the first extension if no preference could be
360 detected.
361  
362 =head2 add_handler
363  
364 $renderer = $renderer->add_handler(epl => sub {...});
365  
366 Register a handler.
367  
368 $renderer->add_handler(foo => sub ($renderer, $c, $output, $options) {
369 ...
370 $$output = 'Hello World!';
371 });
372  
373 =head2 add_helper
374  
375 $renderer = $renderer->add_helper(url_for => sub {...});
376  
377 Register a helper.
378  
379 $renderer->add_helper(foo => sub ($c, @args) {
380 ...
381 });
382  
383 =head2 get_data_template
384  
385 my $template = $renderer->get_data_template({
386 template => 'foo/bar',
387 format => 'html',
388 handler => 'epl'
389 });
390  
391 Return a C<DATA> section template from L</"classes"> for an options hash reference with C<template>, C<format>,
392 C<variant> and C<handler> values, or C<undef> if no template could be found, usually used by handlers.
393  
394 =head2 get_helper
395  
396 my $helper = $renderer->get_helper('url_for');
397  
398 Get a helper by full name, generate a helper dynamically for a prefix, or return C<undef> if no helper or prefix could
399 be found. Generated helpers return a proxy object containing the current controller object and on which nested helpers
400 can be called.
401  
402 =head2 render
403  
404 my ($output, $format) = $renderer->render(Mojolicious::Controller->new);
405  
406 Render output through one of the renderers. See L<Mojolicious::Controller/"render"> for a more user-friendly interface.
407  
408 =head2 respond
409  
410 my $bool = $renderer->respond(Mojolicious::Controller->new, $output, $format);
411 my $bool = $renderer->respond(
412 Mojolicious::Controller->new, $output, $format, $status);
413  
414 Finalize dynamically generated response content and L</"compress"> it if possible.
415  
416 =head2 template_for
417  
418 my $name = $renderer->template_for(Mojolicious::Controller->new);
419  
420 Return default template name for L<Mojolicious::Controller> object, or C<undef> if no name could be generated.
421  
422 =head2 template_handler
423  
424 my $handler = $renderer->template_handler({
425 template => 'foo/bar',
426 format => 'html'
427 });
428  
429 Return handler for an options hash reference with C<template>, C<format> and C<variant> values, or C<undef> if no
430 handler could be found.
431  
432 =head2 template_name
433  
434 my $template = $renderer->template_name({
435 template => 'foo/bar',
436 format => 'html',
437 handler => 'epl'
438 });
439  
440 Return a template name for an options hash reference with C<template>, C<format>, C<variant> and C<handler> values, or
441 C<undef> if no template could be found, usually used by handlers.
442  
443 =head2 template_path
444  
445 my $path = $renderer->template_path({
446 template => 'foo/bar',
447 format => 'html',
448 handler => 'epl'
449 });
450  
451 Return the full template path for an options hash reference with C<template>, C<format>, C<variant> and C<handler>
452 values, or C<undef> if the file does not exist in L</"paths">, usually used by handlers.
453  
454 =head2 warmup
455  
456 $renderer->warmup;
457  
458 Prepare templates from L</"classes"> for future use.
459  
460 =head1 SEE ALSO
461  
462 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
463  
464 =cut
« Mojolicious/Validator/Validation.pm ↑ Summary Mojo/DOM/CSS.pm »