Mojolicious/Plugin/TagHelpers.pm

filter: Statementstmt 52.5% 73 / 139 Branchbran 25.8% 15 / 58 Conditioncond 14.2% 4 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 47.6% 20 / 42 Podpod 100% 1 / 1 totaltotal 39.9% 113 / 283 CC 45 SCAR 24.2
CC
45
Cov
41%
_text_area
42.0
_input
34.0
_option
20.0
CRAP 11.3
« Mojo/Reactor/Poll.pm ↑ Summary Mojo/Message/Request.pm »
1 package Mojolicious::Plugin::TagHelpers;
2 18 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Mojo::DOM::HTML qw(tag_to_html);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 83 ▶sub register {
Subroutine register: called CC=4 SCAR=15.2
Pod documented
9 83 my ($self, $app) = @_;
10  
11 # Text field variations
12 83 my @time = qw(date month time week);
13 83 for my $name (@time, qw(color email number range search tel text url)) {
14 996 ▶ $app->helper("${name}_field" => sub { _input(@_, type => $name) });
Subroutine __ANON__: called CC=1 SCAR=0.0
15 }
16 0 ▶ $app->helper(datetime_field => sub { _input(@_, type => 'datetime-local') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
17  
18 83 my @helpers = (
19 qw(asset_tag csrf_field form_for hidden_field javascript label_for link_to select_field stylesheet submit_button),
20 qw(tag_with_error text_area)
21 );
22 83 $app->helper($_ => __PACKAGE__->can("_$_")) for @helpers;
23  
24 0 ▶ $app->helper(button_to => sub { _button_to(0, @_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 0 ▶ $app->helper(check_box => sub { _input(@_, type => 'checkbox') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 0 ▶ $app->helper(csrf_button_to => sub { _button_to(1, @_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
27 83 ▶ $app->helper(favicon => sub { _favicon(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
28 0 ▶ $app->helper(file_field => sub { _empty_field('file', @_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
29 83 ▶ $app->helper(image => sub { _tag('img', src => _file_url(shift, shift), @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
30 0 ▶ $app->helper(input_tag => sub { _input(@_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
31 0 ▶ $app->helper(password_field => sub { _empty_field('password', @_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
32 0 ▶ $app->helper(radio_button => sub { _input(@_, type => 'radio') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
33  
34 # "t" is just a shortcut for the "tag" helper
35 0 ▶ $app->helper($_ => sub { shift; _tag(@_) }) for qw(t tag);
Subroutine __ANON__: not called CC=1 SCAR=2.6
36 }
37  
38 0 ▶sub _asset_tag {
Subroutine _asset_tag: not called CC=3 SCAR=24.8
39 0 my ($c, $target) = (shift, shift);
40  
41 0 my $url = $c->url_for_asset($target);
42  
43 0 ▶ return $c->helpers->javascript($url, @_) if $target =~ /\.js$/;
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ /\.js$/u 0 0 0
44 0 ▶ return $c->helpers->stylesheet($url, @_) if $target =~ /\.css$/;
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ /\.css$/u 0 0 0
45 0 return $c->helpers->image($url, @_);
46 }
47  
48 0 ▶sub _button_to {
Subroutine _button_to: not called CC=2 SCAR=17.9
49 0 my ($csrf, $c, $text) = (shift, shift, shift);
50 0 ▶ my $prefix = $csrf ? _csrf_field($c) : '';
Branch: 1 branch
BranchTrueFalseTotal
$csrf ? : 0 0 0
51 0 ▶ return _form_for($c, @_, sub { $prefix . _submit_button($c, $text) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 }
53  
54 0 ▶sub _csrf_field {
Subroutine _csrf_field: not called CC=1 SCAR=6.9
55 0 my $c = shift;
56 0 return _hidden_field($c, csrf_token => $c->helpers->csrf_token, @_);
57 }
58  
59 0 ▶sub _empty_field {
Subroutine _empty_field: not called CC=1 SCAR=6.9
60 0 my ($type, $c, $name) = (shift, shift, shift);
61 0 return _validation($c, $name, 'input', name => $name, @_, type => $type);
62 }
63  
64 17 ▶sub _favicon {
Subroutine _favicon: called CC=2 SCAR=7.2
65 17 my ($c, $file) = @_;
66 17 ▶ return _tag('link', rel => 'icon', href => _file_url($c, $file // 'favicon.ico'));
Condition50%1/2
$file // "favicon.ico"
l!l
017
MC/DC0%0/1
$file // "favicon.ico"
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
67 }
68  
69 100 ▶sub _file_url {
Subroutine _file_url: called CC=3 SCAR=13.1
70 100 my ($c, $url) = @_;
71 100 ▶ return blessed $url && $url->isa('Mojo::URL') ? $url : $c->url_for_file($url);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($url) && $url->isa('Mojo::URL') ? : 0 100 100
Condition33%1/3
builtin::blessed($url) and $url->isa("Mojo::URL")
!ll&&!rl&&r
10000
MC/DC0%0/2
builtin::blessed($url) and $url->isa("Mojo::URL")
builtin::blessed($url) $url->isa("Mojo::URL")
Truth table33%1/3
A and B
A= builtin::blessed($url)B= $url->isa("Mojo::URL")
ABresult
0X0
100
111
72 }
73  
74 2 ▶sub _form_for {
Subroutine _form_for: called CC=6 SCAR=19.6
75 2 my ($c, @url) = (shift, shift);
76 2 ▶ push @url, shift if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 2 2
77  
78 # Method detection
79 2 my $r = $c->app->routes->lookup($url[0]);
80 2 ▶ my $method = $r ? $r->suggested_method : 'GET';
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 2 2
81 2 ▶ my @post = $method ne 'GET' ? (method => 'POST') : ();
Branch: 1 branch
BranchTrueFalseTotal
$method ne 'GET' ? : 0 2 2
82  
83 2 my $url = $c->url_for(@url);
84 2 ▶ $url->query({_method => $method}) if @post && $method ne 'POST';
Branch: 1 branch
BranchTrueFalseTotal
if @post and $method ne "POST" 0 2 2
Condition33%1/3
@post and $method ne "POST"
!ll&&!rl&&r
200
MC/DC0%0/2
@post and $method ne "POST"
@post $method ne "POST"
Truth table33%1/3
A and B
A= @postB= $method ne "POST"
ABresult
0X0
100
111
85 2 return _tag('form', action => $url, @post, @_);
86 }
87  
88 22 ▶sub _hidden_field {
Subroutine _hidden_field: called CC=1 SCAR=0.0
89 22 my ($c, $name, $value) = (shift, shift, shift);
90 22 return _tag('input', name => $name, value => $value, @_, type => 'hidden');
91 }
92  
93 11 ▶sub _input {
Subroutine _input: called CC=8 SCAR=35.3
94 11 my ($c, $name) = (shift, shift);
95 11 ▶ my %attrs = @_ % 2 ? (value => shift, @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 11 11
96  
97 11 ▶ if (my @values = @{$c->every_param($name)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@values) = @{$c->every_param($name);}) 0 11 11
98  
99 # Checkbox or radiobutton
100 0 ▶ my $type = $attrs{type} || '';
Condition0%0/2
$attrs{'type'} || ''
l!l
00
MC/DC0%0/1
$attrs{'type'} || ''
$attrs{'type'}
Truth table0%0/2
A
A= $attrs{'type'}
Aresult
11
00
101 0 ▶ if ($type eq 'checkbox' || $type eq 'radio') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'checkbox' or $type eq 'radio') { } 0 0 0
Condition0%0/3
$type eq "checkbox" or $type eq "radio"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "checkbox" or $type eq "radio"
$type eq "checkbox" $type eq "radio"
Truth table0%0/3
A or B
A= $type eq "checkbox"B= $type eq "radio"
ABresult
1X1
011
000
102 0 ▶ my $value = $attrs{value} // 'on';
Condition0%0/2
$attrs{"value"} // "on"
l!l
00
MC/DC0%0/1
$attrs{"value"} // "on"
$attrs{"value"}
Truth table0%0/2
A
A= $attrs{"value"}
Aresult
11
00
103 0 delete $attrs{checked};
104 0 ▶ $attrs{checked} = undef if grep { $_ eq $value } @values;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $value;} @values 0 0 0
105 }
106  
107 # Others
108 0 else { $attrs{value} = $values[-1] }
109 }
110  
111 11 return _validation($c, $name, 'input', name => $name, %attrs);
112 }
113  
114 44 ▶sub _javascript {
Subroutine _javascript: called CC=3 SCAR=11.3
115 44 my $c = shift;
116 44 ▶ my $content = ref $_[-1] eq 'CODE' ? "//<![CDATA[\n" . pop->() . "\n//]]>" : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 44 44
117 44 ▶ my @src = @_ % 2 ? (src => _file_url($c, shift)) : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 44 0 44
118 44 ▶ return _tag('script', @src, @_, sub {$content});
Subroutine __ANON__: called CC=1 SCAR=0.0
119 }
120  
121 0 ▶sub _label_for {
Subroutine _label_for: not called CC=2 SCAR=17.9
122 0 my ($c, $name) = (shift, shift);
123 0 ▶ my $content = ref $_[-1] eq 'CODE' ? pop : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
124 0 return _validation($c, $name, 'label', for => $name, @_, $content);
125 }
126  
127 8 ▶sub _link_to {
Subroutine _link_to: called CC=3 SCAR=11.0
128 8 my ($c, $content) = (shift, shift);
129 8 my @url = ($content);
130  
131 # Content
132 8 ▶ unless (ref $_[-1] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[-1] eq "CODE") 2 6 8
133 2 @url = (shift);
134 2 push @_, $content;
135 }
136  
137 # Captures
138 8 ▶ push @url, shift if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 8 8
139  
140 8 return _tag('a', href => $c->url_for(@url), @_);
141 }
142  
143 0 ▶sub _option {
Subroutine _option: not called CC=4 SCAR=30.0
144 0 my ($values, $pair) = @_;
145  
146 0 ▶ $pair = [$pair => $pair] unless ref $pair eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pair eq "ARRAY" 0 0 0
147 0 my %attrs = (value => $pair->[1], @$pair[2 .. $#$pair]);
148 0 ▶ delete $attrs{selected} if keys %$values;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$values 0 0 0
149 0 ▶ $attrs{selected} = undef if $values->{$pair->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $$values{$pair->[1]} 0 0 0
150  
151 0 return _tag('option', %attrs, $pair->[0]);
152 }
153  
154 0 ▶sub _select_field {
Subroutine _select_field: not called CC=4 SCAR=30.0
155 0 my ($c, $name, $options, %attrs) = (shift, shift, shift, @_);
156  
157 0 my %values = map { $_ => 1 } grep {defined} @{$c->every_param($name)};
158  
159 0 my $groups = '';
160 0 for my $group (@$options) {
161  
162 # "optgroup" tag
163 0 ▶ if (blessed $group && $group->isa('Mojo::Collection')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($group) and $group->isa('Mojo::Collection')) { } 0 0 0
Condition0%0/3
builtin::blessed($group) and $group->isa("Mojo::Collection")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($group) and $group->isa("Mojo::Collection")
builtin::blessed($group) $group->isa("Mojo::Collection")
Truth table0%0/3
A and B
A= builtin::blessed($group)B= $group->isa("Mojo::Collection")
ABresult
0X0
100
111
164 0 my ($label, $values, %attrs) = @$group;
165 0 my $content = join '', map { _option(\%values, $_) } @$values;
166 0 ▶ $groups .= _tag('optgroup', label => $label, %attrs, sub {$content});
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 }
168  
169 # "option" tag
170 0 else { $groups .= _option(\%values, $group) }
171 }
172  
173 0 ▶ return _validation($c, $name, 'select', name => $name, %attrs, sub {$groups});
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 }
175  
176 33 ▶sub _stylesheet {
Subroutine _stylesheet: called CC=3 SCAR=12.0
177 33 my $c = shift;
178 33 ▶ my $content = ref $_[-1] eq 'CODE' ? "/*<![CDATA[*/\n" . pop->() . "\n/*]]>*/" : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 33 33
179 0 ▶ return _tag('style', @_, sub {$content}) unless @_ % 2;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 0 33 33
180 33 return _tag('link', rel => 'stylesheet', href => _file_url($c, shift), @_);
181 }
182  
183 2 ▶sub _submit_button {
Subroutine _submit_button: called CC=2 SCAR=7.2
184 2 ▶ my ($c, $value) = (shift, shift // 'Ok');
Condition50%1/2
shift() // "Ok"
l!l
20
MC/DC0%0/1
shift() // "Ok"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
185 2 return _tag('input', value => $value, @_, type => 'submit');
186 }
187  
188 145 ▶sub _tag { Mojo::ByteStream->new(tag_to_html(@_)) }
Subroutine _tag: called CC=1 SCAR=0.0
189  
190 0 ▶sub _tag_with_error {
Subroutine _tag_with_error: not called CC=4 SCAR=30.0
191 0 my ($c, $tag) = (shift, shift);
192 0 ▶ my ($content, %attrs) = (@_ % 2 ? pop : undef, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
193 0 ▶ $attrs{class} .= $attrs{class} ? ' field-with-error' : 'field-with-error';
Branch: 1 branch
BranchTrueFalseTotal
$attrs{'class'} ? : 0 0 0
194 0 ▶ return _tag($tag, %attrs, defined $content ? $content : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 0 0 0
195 }
196  
197 0 ▶sub _text_area {
Subroutine _text_area: not called CC=6 SCAR=37.4
198 0 my ($c, $name) = (shift, shift);
199  
200 0 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
201 0 ▶ my $content = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
202 0 ▶ $content = $c->param($name) // $content // $cb // '';
Condition0%0/8
(($c->param($name) // $content) // $cb) // ""
l!l
00
($c->param($name) // $content) // $cb
l!l&&r!l&&!r
000
$c->param($name) // $content
l!l&&r!l&&!r
000
MC/DC0%0/3
(($c->param($name) // $content) // $cb) // ""
$c->param($name) $content $cb
Truth table0%0/4
A // B // C
A= $c->param($name)B= $contentC= $cb
ABCresult
1XX1
01X1
0011
0000
203  
204 0 return _validation($c, $name, 'textarea', name => $name, @_, $content);
205 }
206  
207 11 ▶sub _validation {
Subroutine _validation: called CC=2 SCAR=8.1
208 11 my ($c, $name) = (shift, shift);
209 11 ▶ return _tag(@_) unless $c->helpers->validation->has_error($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $c->helpers->validation->has_error($name) 11 0 11
210 0 return $c->helpers->tag_with_error(@_);
211 }
212  
213 18 1;
214  
215 =encoding utf8
216  
217 =head1 NAME
218  
219 Mojolicious::Plugin::TagHelpers - Tag helpers plugin
220  
221 =head1 SYNOPSIS
222  
223 # Mojolicious
224 $app->plugin('TagHelpers');
225  
226 # Mojolicious::Lite
227 plugin 'TagHelpers';
228  
229 =head1 DESCRIPTION
230  
231 L<Mojolicious::Plugin::TagHelpers> is a collection of HTML tag helpers for L<Mojolicious>, based on the L<HTML Living
232 Standard|https://html.spec.whatwg.org>.
233  
234 Most form helpers can automatically pick up previous input values and will show them as default. You can also use
235 L<Mojolicious::Plugin::DefaultHelpers/"param"> to set them manually and let necessary attributes always be generated
236 automatically.
237  
238 % param country => 'germany' unless param 'country';
239 <%= radio_button country => 'germany' %> Germany
240 <%= radio_button country => 'france' %> France
241 <%= radio_button country => 'uk' %> UK
242  
243 For fields that failed validation with L<Mojolicious::Plugin::DefaultHelpers/"validation"> the C<field-with-error>
244 class will be automatically added through L</"tag_with_error">, to make styling with CSS easier.
245  
246 <input class="field-with-error" name="age" type="text" value="250">
247  
248 This is a core plugin, that means it is always enabled and its code a good example for learning how to build new
249 plugins, you're welcome to fork it.
250  
251 See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available by default.
252  
253 =head1 HELPERS
254  
255 L<Mojolicious::Plugin::TagHelpers> implements the following helpers.
256  
257 =head2 asset_tag
258  
259 %= asset_tag '/app.js'
260 %= asset_tag '/app.js', async => 'async'
261  
262 Generate C<script>, C<link> or C<img> tag for static asset.
263  
264 =head2 button_to
265  
266 %= button_to Test => 'some_get_route'
267 %= button_to Test => some_get_route => {id => 23} => (class => 'menu')
268 %= button_to Test => 'http://example.com/test' => (class => 'menu')
269 %= button_to Remove => 'some_delete_route'
270  
271 Generate portable C<form> tag with L</"form_for">, containing a single button.
272  
273 <form action="/path/to/get/route">
274 <input type="submit" value="Test">
275 </form>
276 <form action="/path/to/get/route/23" class="menu">
277 <input type="submit" value="Test">
278 </form>
279 <form action="http://example.com/test" class="menu">
280 <input type="submit" value="Test">
281 </form>
282 <form action="/path/to/delete/route?_method=DELETE" method="POST">
283 <input type="submit" value="Remove">
284 </form>
285  
286 =head2 check_box
287  
288 %= check_box 'employed'
289 %= check_box employed => 1
290 %= check_box employed => 1, checked => undef, id => 'foo'
291  
292 Generate C<input> tag of type C<checkbox>. Previous input values will automatically get picked up and shown as default.
293  
294 <input name="employed" type="checkbox">
295 <input name="employed" type="checkbox" value="1">
296 <input checked id="foo" name="employed" type="checkbox" value="1">
297  
298 =head2 color_field
299  
300 %= color_field 'background'
301 %= color_field background => '#ffffff'
302 %= color_field background => '#ffffff', id => 'foo'
303  
304 Generate C<input> tag of type C<color>. Previous input values will automatically get picked up and shown as default.
305  
306 <input name="background" type="color">
307 <input name="background" type="color" value="#ffffff">
308 <input id="foo" name="background" type="color" value="#ffffff">
309  
310 =head2 csrf_button_to
311  
312 %= csrf_button_to Remove => 'some_delete_route'
313  
314 Same as L</"button_to">, but also includes a L</"csrf_field">.
315  
316 <form action="/path/to/delete/route?_method=DELETE" method="POST">
317 <input name="csrf_token" type="hidden" value="fa6a08...">
318 <input type="submit" value="Remove">
319 </form>
320  
321 =head2 csrf_field
322  
323 %= csrf_field
324  
325 Generate C<input> tag of type C<hidden> with L<Mojolicious::Plugin::DefaultHelpers/"csrf_token">.
326  
327 <input name="csrf_token" type="hidden" value="fa6a08...">
328  
329 =head2 date_field
330  
331 %= date_field 'end'
332 %= date_field end => '2012-12-21'
333 %= date_field end => '2012-12-21', id => 'foo'
334  
335 Generate C<input> tag of type C<date>. Previous input values will automatically get picked up and shown as default.
336  
337 <input name="end" type="date">
338 <input name="end" type="date" value="2012-12-21">
339 <input id="foo" name="end" type="date" value="2012-12-21">
340  
341 =head2 datetime_field
342  
343 %= datetime_field 'end'
344 %= datetime_field end => '2012-12-21T23:59:59'
345 %= datetime_field end => '2012-12-21T23:59:59', id => 'foo'
346  
347 Generate C<input> tag of type C<datetime-local>. Previous input values will automatically get picked up and shown as
348 default.
349  
350 <input name="end" type="datetime-local">
351 <input name="end" type="datetime-local" value="2012-12-21T23:59:59">
352 <input id="foo" name="end" type="datetime-local" value="2012-12-21T23:59:59">
353  
354 =head2 email_field
355  
356 %= email_field 'notify'
357 %= email_field notify => 'nospam@example.com'
358 %= email_field notify => 'nospam@example.com', id => 'foo'
359  
360 Generate C<input> tag of type C<email>. Previous input values will automatically get picked up and shown as default.
361  
362 <input name="notify" type="email">
363 <input name="notify" type="email" value="nospam@example.com">
364 <input id="foo" name="notify" type="email" value="nospam@example.com">
365  
366 =head2 favicon
367  
368 %= favicon
369 %= favicon '/favicon.ico';
370  
371 Generate C<link> tag for favicon, defaulting to the one that comes bundled with Mojolicious.
372  
373 <link rel="icon" href="/mojo/favicon.ico">
374 <link rel="icon" href="/favicon.ico">
375  
376 =head2 file_field
377  
378 %= file_field 'avatar'
379 %= file_field 'avatar', id => 'foo'
380  
381 Generate C<input> tag of type C<file>.
382  
383 <input name="avatar" type="file">
384 <input id="foo" name="avatar" type="file">
385  
386 =head2 form_for
387  
388 %= form_for login => begin
389 %= text_field 'first_name'
390 %= submit_button
391 % end
392 %= form_for login => {format => 'txt'} => (method => 'POST') => begin
393 %= text_field 'first_name'
394 %= submit_button
395 % end
396 %= form_for '/login' => (enctype => 'multipart/form-data') => begin
397 %= text_field 'first_name', disabled => 'disabled'
398 %= submit_button
399 % end
400 %= form_for 'http://example.com/login' => (method => 'POST') => begin
401 %= text_field 'first_name'
402 %= submit_button
403 % end
404 %= form_for some_delete_route => begin
405 %= submit_button 'Remove'
406 % end
407  
408 Generate portable C<form> tag with L<Mojolicious::Controller/"url_for">. For routes that do not allow C<GET>, a
409 C<method> attribute with the value C<POST> will be automatically added. And for methods other than C<GET> or C<POST>,
410 an C<_method> query parameter will be added as well.
411  
412 <form action="/path/to/login">
413 <input name="first_name" type="text">
414 <input type="submit" value="Ok">
415 </form>
416 <form action="/path/to/login.txt" method="POST">
417 <input name="first_name" type="text">
418 <input type="submit" value="Ok">
419 </form>
420 <form action="/path/to/login" enctype="multipart/form-data">
421 <input disabled="disabled" name="first_name" type="text">
422 <input type="submit" value="Ok">
423 </form>
424 <form action="http://example.com/login" method="POST">
425 <input name="first_name" type="text">
426 <input type="submit" value="Ok">
427 </form>
428 <form action="/path/to/delete/route?_method=DELETE" method="POST">
429 <input type="submit" value="Remove">
430 </form>
431  
432 =head2 hidden_field
433  
434 %= hidden_field foo => 'bar'
435 %= hidden_field foo => 'bar', id => 'bar'
436  
437 Generate C<input> tag of type C<hidden>.
438  
439 <input name="foo" type="hidden" value="bar">
440 <input id="bar" name="foo" type="hidden" value="bar">
441  
442 =head2 image
443  
444 %= image '/images/foo.png'
445 %= image '/images/foo.png', alt => 'Foo'
446  
447 Generate portable C<img> tag.
448  
449 <img src="/path/to/images/foo.png">
450 <img alt="Foo" src="/path/to/images/foo.png">
451  
452 =head2 input_tag
453  
454 %= input_tag 'first_name'
455 %= input_tag first_name => 'Default'
456 %= input_tag 'employed', type => 'checkbox'
457  
458 Generate C<input> tag. Previous input values will automatically get picked up and shown as default.
459  
460 <input name="first_name">
461 <input name="first_name" value="Default">
462 <input name="employed" type="checkbox">
463  
464 =head2 javascript
465  
466 %= javascript '/script.js'
467 %= javascript '/script.js', defer => undef
468 %= javascript begin
469 const a = 'b';
470 % end
471  
472 Generate portable C<script> tag for JavaScript asset.
473  
474 <script src="/path/to/script.js"></script>
475 <script defer src="/path/to/script.js"></script>
476 <script><![CDATA[
477 const a = 'b';
478 ]]></script>
479  
480 =head2 label_for
481  
482 %= label_for first_name => 'First name'
483 %= label_for first_name => 'First name', class => 'user'
484 %= label_for first_name => begin
485 First name
486 % end
487 %= label_for first_name => (class => 'user') => begin
488 First name
489 % end
490  
491 Generate C<label> tag.
492  
493 <label for="first_name">First name</label>
494 <label class="user" for="first_name">First name</label>
495 <label for="first_name">
496 First name
497 </label>
498 <label class="user" for="first_name">
499 First name
500 </label>
501  
502 =head2 link_to
503  
504 %= link_to Home => 'index'
505 %= link_to Home => 'index' => {format => 'txt'} => (class => 'menu')
506 %= link_to index => {format => 'txt'} => (class => 'menu') => begin
507 Home
508 % end
509 %= link_to Contact => 'mailto:sri@example.com'
510 <%= link_to index => begin %>Home<% end %>
511 <%= link_to '/file.txt' => begin %>File<% end %>
512 <%= link_to 'https://mojolicious.org' => begin %>Mojolicious<% end %>
513 <%= link_to url_for->query(foo => 'bar')->to_abs => begin %>Retry<% end %>
514  
515 Generate portable C<a> tag with L<Mojolicious::Controller/"url_for">, defaults to using the capitalized link target as
516 content.
517  
518 <a href="/path/to/index">Home</a>
519 <a class="menu" href="/path/to/index.txt">Home</a>
520 <a class="menu" href="/path/to/index.txt">
521 Home
522 </a>
523 <a href="mailto:sri@example.com">Contact</a>
524 <a href="/path/to/index">Home</a>
525 <a href="/path/to/file.txt">File</a>
526 <a href="https://mojolicious.org">Mojolicious</a>
527 <a href="http://127.0.0.1:3000/current/path?foo=bar">Retry</a>
528  
529 The first argument to C<link_to> is the link content, except when the
530 final argument is Perl code such as a template block (created with the
531 C<begin> and C<end> keywords); in that case, the link content is
532 omitted at the start of the argument list, and the block will become
533 the link content.
534  
535 =head2 month_field
536  
537 %= month_field 'vacation'
538 %= month_field vacation => '2012-12'
539 %= month_field vacation => '2012-12', id => 'foo'
540  
541 Generate C<input> tag of type C<month>. Previous input values will automatically get picked up and shown as default.
542  
543 <input name="vacation" type="month">
544 <input name="vacation" type="month" value="2012-12">
545 <input id="foo" name="vacation" type="month" value="2012-12">
546  
547 =head2 number_field
548  
549 %= number_field 'age'
550 %= number_field age => 25
551 %= number_field age => 25, id => 'foo', min => 0, max => 200
552  
553 Generate C<input> tag of type C<number>. Previous input values will automatically get picked up and shown as default.
554  
555 <input name="age" type="number">
556 <input name="age" type="number" value="25">
557 <input id="foo" max="200" min="0" name="age" type="number" value="25">
558  
559 =head2 password_field
560  
561 %= password_field 'pass'
562 %= password_field 'pass', id => 'foo'
563  
564 Generate C<input> tag of type C<password>.
565  
566 <input name="pass" type="password">
567 <input id="foo" name="pass" type="password">
568  
569 =head2 radio_button
570  
571 %= radio_button 'test'
572 %= radio_button country => 'germany'
573 %= radio_button country => 'germany', checked => undef, id => 'foo'
574  
575 Generate C<input> tag of type C<radio>. Previous input values will automatically get picked up and shown as default.
576  
577 <input name="test" type="radio">
578 <input name="country" type="radio" value="germany">
579 <input checked id="foo" name="country" type="radio" value="germany">
580  
581 =head2 range_field
582  
583 %= range_field 'age'
584 %= range_field age => 25
585 %= range_field age => 25, id => 'foo', min => 0, max => 200
586  
587 Generate C<input> tag of type C<range>. Previous input values will automatically get picked up and shown as default.
588  
589 <input name="age" type="range">
590 <input name="age" type="range" value="25">
591 <input id="foo" max="200" min="200" name="age" type="range" value="25">
592  
593 =head2 search_field
594  
595 %= search_field 'q'
596 %= search_field q => 'perl'
597 %= search_field q => 'perl', id => 'foo'
598  
599 Generate C<input> tag of type C<search>. Previous input values will automatically get picked up and shown as default.
600  
601 <input name="q" type="search">
602 <input name="q" type="search" value="perl">
603 <input id="foo" name="q" type="search" value="perl">
604  
605 =head2 select_field
606  
607 %= select_field country => ['de', 'en']
608 %= select_field country => [[Germany => 'de'], 'en'], id => 'eu'
609 %= select_field country => [[Germany => 'de', selected => 'selected'], 'en']
610 %= select_field country => [c(EU => [[Germany => 'de'], 'en'], id => 'eu')]
611 %= select_field country => [c(EU => ['de', 'en']), c(Asia => ['cn', 'jp'])]
612  
613 Generate C<select> and C<option> tags from array references and C<optgroup> tags from L<Mojo::Collection> objects.
614 Previous input values will automatically get picked up and shown as default.
615  
616 <select name="country">
617 <option value="de">de</option>
618 <option value="en">en</option>
619 </select>
620 <select id="eu" name="country">
621 <option value="de">Germany</option>
622 <option value="en">en</option>
623 </select>
624 <select name="country">
625 <option selected="selected" value="de">Germany</option>
626 <option value="en">en</option>
627 </select>
628 <select name="country">
629 <optgroup id="eu" label="EU">
630 <option value="de">Germany</option>
631 <option value="en">en</option>
632 </optgroup>
633 </select>
634 <select name="country">
635 <optgroup label="EU">
636 <option value="de">de</option>
637 <option value="en">en</option>
638 </optgroup>
639 <optgroup label="Asia">
640 <option value="cn">cn</option>
641 <option value="jp">jp</option>
642 </optgroup>
643 </select>
644  
645 =head2 stylesheet
646  
647 %= stylesheet '/foo.css'
648 %= stylesheet '/foo.css', title => 'Foo style'
649 %= stylesheet begin
650 body {color: #000}
651 % end
652  
653 Generate portable C<style> or C<link> tag for CSS asset.
654  
655 <link href="/path/to/foo.css" rel="stylesheet">
656 <link href="/path/to/foo.css" rel="stylesheet" title="Foo style">
657 <style><![CDATA[
658 body {color: #000}
659 ]]></style>
660  
661 =head2 submit_button
662  
663 %= submit_button
664 %= submit_button 'Ok!', id => 'foo'
665  
666 Generate C<input> tag of type C<submit>.
667  
668 <input type="submit" value="Ok">
669 <input id="foo" type="submit" value="Ok!">
670  
671 =head2 t
672  
673 %= t div => 'test & 123'
674  
675 Alias for L</"tag">.
676  
677 <div>test &amp; 123</div>
678  
679 =head2 tag
680  
681 %= tag 'br'
682 %= tag 'div'
683 %= tag 'div', id => 'foo', hidden => undef
684 %= tag 'div', 'test & 123'
685 %= tag 'div', id => 'foo', 'test & 123'
686 %= tag 'div', data => {my_id => 1, Name => 'test'}, 'test & 123'
687 %= tag div => begin
688 test & 123
689 % end
690 <%= tag div => (id => 'foo') => begin %>test & 123<% end %>
691  
692 Alias for L<Mojo::DOM/"new_tag">.
693  
694 <br>
695 <div></div>
696 <div id="foo" hidden></div>
697 <div>test &amp; 123</div>
698 <div id="foo">test &amp; 123</div>
699 <div data-my-id="1" data-name="test">test &amp; 123</div>
700 <div>
701 test & 123
702 </div>
703 <div id="foo">test & 123</div>
704  
705 Very useful for reuse in more specific tag helpers.
706  
707 my $output = $c->tag('meta');
708 my $output = $c->tag('meta', charset => 'UTF-8');
709 my $output = $c->tag('div', '<p>This will be escaped</p>');
710 my $output = $c->tag('div', sub { '<p>This will not be escaped</p>' });
711  
712 Results are automatically wrapped in L<Mojo::ByteStream> objects to prevent accidental double escaping in C<ep>
713 templates.
714  
715 =head2 tag_with_error
716  
717 %= tag_with_error 'input', class => 'foo'
718  
719 Same as L</"tag">, but adds the class C<field-with-error>.
720  
721 <input class="foo field-with-error">
722  
723 =head2 tel_field
724  
725 %= tel_field 'work'
726 %= tel_field work => '123456789'
727 %= tel_field work => '123456789', id => 'foo'
728  
729 Generate C<input> tag of type C<tel>. Previous input values will automatically get picked up and shown as default.
730  
731 <input name="work" type="tel">
732 <input name="work" type="tel" value="123456789">
733 <input id="foo" name="work" type="tel" value="123456789">
734  
735 =head2 text_area
736  
737 %= text_area 'story'
738 %= text_area 'story', cols => 40
739 %= text_area story => 'Default', cols => 40
740 %= text_area story => (cols => 40) => begin
741 Default
742 % end
743  
744 Generate C<textarea> tag. Previous input values will automatically get picked up and shown as default.
745  
746 <textarea name="story"></textarea>
747 <textarea cols="40" name="story"></textarea>
748 <textarea cols="40" name="story">Default</textarea>
749 <textarea cols="40" name="story">
750 Default
751 </textarea>
752  
753 =head2 text_field
754  
755 %= text_field 'first_name'
756 %= text_field first_name => 'Default'
757 %= text_field first_name => 'Default', class => 'user'
758  
759 Generate C<input> tag of type C<text>. Previous input values will automatically get picked up and shown as default.
760  
761 <input name="first_name" type="text">
762 <input name="first_name" type="text" value="Default">
763 <input class="user" name="first_name" type="text" value="Default">
764  
765 =head2 time_field
766  
767 %= time_field 'start'
768 %= time_field start => '23:59:59'
769 %= time_field start => '23:59:59', id => 'foo'
770  
771 Generate C<input> tag of type C<time>. Previous input values will automatically get picked up and shown as default.
772  
773 <input name="start" type="time">
774 <input name="start" type="time" value="23:59:59">
775 <input id="foo" name="start" type="time" value="23:59:59">
776  
777 =head2 url_field
778  
779 %= url_field 'address'
780 %= url_field address => 'https://mojolicious.org'
781 %= url_field address => 'https://mojolicious.org', id => 'foo'
782  
783 Generate C<input> tag of type C<url>. Previous input values will automatically get picked up and shown as default.
784  
785 <input name="address" type="url">
786 <input name="address" type="url" value="https://mojolicious.org">
787 <input id="foo" name="address" type="url" value="https://mojolicious.org">
788  
789 =head2 week_field
790  
791 %= week_field 'vacation'
792 %= week_field vacation => '2012-W17'
793 %= week_field vacation => '2012-W17', id => 'foo'
794  
795 Generate C<input> tag of type C<week>. Previous input values will automatically get picked up and shown as default.
796  
797 <input name="vacation" type="week">
798 <input name="vacation" type="week" value="2012-W17">
799 <input id="foo" name="vacation" type="week" value="2012-W17">
800  
801 =head1 METHODS
802  
803 L<Mojolicious::Plugin::TagHelpers> inherits all methods from L<Mojolicious::Plugin> and implements the following new
804 ones.
805  
806 =head2 register
807  
808 $plugin->register(Mojolicious->new);
809  
810 Register helpers in L<Mojolicious> application.
811  
812 =head1 SEE ALSO
813  
814 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
815  
816 =cut
« Mojo/Reactor/Poll.pm ↑ Summary Mojo/Message/Request.pm »