1Mojolicious::Plugin::TaUgsHeerlpCeornst(r3i)buted Perl DMoocjuomleinctiaotuiso:n:Plugin::TagHelpers(3)
2
3
4

NAME

6       Mojolicious::Plugin::TagHelpers - Tag helpers plugin
7

SYNOPSIS

9         # Mojolicious
10         $app->plugin('TagHelpers');
11
12         # Mojolicious::Lite
13         plugin 'TagHelpers';
14

DESCRIPTION

16       Mojolicious::Plugin::TagHelpers is a collection of HTML tag helpers for
17       Mojolicious, based on the HTML Living Standard
18       <https://html.spec.whatwg.org>.
19
20       Most form helpers can automatically pick up previous input values and
21       will show them as default. You can also use "param" in
22       Mojolicious::Plugin::DefaultHelpers to set them manually and let
23       necessary attributes always be generated automatically.
24
25         % param country => 'germany' unless param 'country';
26         <%= radio_button country => 'germany' %> Germany
27         <%= radio_button country => 'france'  %> France
28         <%= radio_button country => 'uk'      %> UK
29
30       For fields that failed validation with "validation" in
31       Mojolicious::Plugin::DefaultHelpers the "field-with-error" class will
32       be automatically added through "tag_with_error", to make styling with
33       CSS easier.
34
35         <input class="field-with-error" name="age" type="text" value="250">
36
37       This is a core plugin, that means it is always enabled and its code a
38       good example for learning how to build new plugins, you're welcome to
39       fork it.
40
41       See "PLUGINS" in Mojolicious::Plugins for a list of plugins that are
42       available by default.
43

HELPERS

45       Mojolicious::Plugin::TagHelpers implements the following helpers.
46
47   asset_tag
48         %= asset_tag '/app.js'
49         %= asset_tag '/app.js', async => 'async'
50
51       Generate "script", "link" or "img" tag for static asset.
52
53   button_to
54         %= button_to Test => 'some_get_route'
55         %= button_to Test => some_get_route => {id => 23} => (class => 'menu')
56         %= button_to Test => 'http://example.com/test' => (class => 'menu')
57         %= button_to Remove => 'some_delete_route'
58
59       Generate portable "form" tag with "form_for", containing a single
60       button.
61
62         <form action="/path/to/get/route">
63           <input type="submit" value="Test">
64         </form>
65         <form action="/path/to/get/route/23" class="menu">
66           <input type="submit" value="Test">
67         </form>
68         <form action="http://example.com/test" class="menu">
69           <input type="submit" value="Test">
70         </form>
71         <form action="/path/to/delete/route?_method=DELETE" method="POST">
72           <input type="submit" value="Remove">
73         </form>
74
75   check_box
76         %= check_box 'employed'
77         %= check_box employed => 1
78         %= check_box employed => 1, checked => undef, id => 'foo'
79
80       Generate "input" tag of type "checkbox". Previous input values will
81       automatically get picked up and shown as default.
82
83         <input name="employed" type="checkbox">
84         <input name="employed" type="checkbox" value="1">
85         <input checked id="foo" name="employed" type="checkbox" value="1">
86
87   color_field
88         %= color_field 'background'
89         %= color_field background => '#ffffff'
90         %= color_field background => '#ffffff', id => 'foo'
91
92       Generate "input" tag of type "color". Previous input values will
93       automatically get picked up and shown as default.
94
95         <input name="background" type="color">
96         <input name="background" type="color" value="#ffffff">
97         <input id="foo" name="background" type="color" value="#ffffff">
98
99   csrf_button_to
100         %= csrf_button_to Remove => 'some_delete_route'
101
102       Same as "button_to", but also includes a "csrf_field".
103
104         <form action="/path/to/delete/route?_method=DELETE" method="POST">
105           <input name="csrf_token" type="hidden" value="fa6a08...">
106           <input type="submit" value="Remove">
107         </form>
108
109   csrf_field
110         %= csrf_field
111
112       Generate "input" tag of type "hidden" with "csrf_token" in
113       Mojolicious::Plugin::DefaultHelpers.
114
115         <input name="csrf_token" type="hidden" value="fa6a08...">
116
117   date_field
118         %= date_field 'end'
119         %= date_field end => '2012-12-21'
120         %= date_field end => '2012-12-21', id => 'foo'
121
122       Generate "input" tag of type "date". Previous input values will
123       automatically get picked up and shown as default.
124
125         <input name="end" type="date">
126         <input name="end" type="date" value="2012-12-21">
127         <input id="foo" name="end" type="date" value="2012-12-21">
128
129   datetime_field
130         %= datetime_field 'end'
131         %= datetime_field end => '2012-12-21T23:59:59'
132         %= datetime_field end => '2012-12-21T23:59:59', id => 'foo'
133
134       Generate "input" tag of type "datetime-local". Previous input values
135       will automatically get picked up and shown as default.
136
137         <input name="end" type="datetime-local">
138         <input name="end" type="datetime-local" value="2012-12-21T23:59:59">
139         <input id="foo" name="end" type="datetime-local" value="2012-12-21T23:59:59">
140
141   email_field
142         %= email_field 'notify'
143         %= email_field notify => 'nospam@example.com'
144         %= email_field notify => 'nospam@example.com', id => 'foo'
145
146       Generate "input" tag of type "email". Previous input values will
147       automatically get picked up and shown as default.
148
149         <input name="notify" type="email">
150         <input name="notify" type="email" value="nospam@example.com">
151         <input id="foo" name="notify" type="email" value="nospam@example.com">
152
153   file_field
154         %= file_field 'avatar'
155         %= file_field 'avatar', id => 'foo'
156
157       Generate "input" tag of type "file".
158
159         <input name="avatar" type="file">
160         <input id="foo" name="avatar" type="file">
161
162   form_for
163         %= form_for login => begin
164           %= text_field 'first_name'
165           %= submit_button
166         % end
167         %= form_for login => {format => 'txt'} => (method => 'POST') => begin
168           %= text_field 'first_name'
169           %= submit_button
170         % end
171         %= form_for '/login' => (enctype => 'multipart/form-data') => begin
172           %= text_field 'first_name', disabled => 'disabled'
173           %= submit_button
174         % end
175         %= form_for 'http://example.com/login' => (method => 'POST') => begin
176           %= text_field 'first_name'
177           %= submit_button
178         % end
179         %= form_for some_delete_route => begin
180           %= submit_button 'Remove'
181         % end
182
183       Generate portable "form" tag with "url_for" in Mojolicious::Controller.
184       For routes that do not allow "GET", a "method" attribute with the value
185       "POST" will be automatically added. And for methods other than "GET" or
186       "POST", an "_method" query parameter will be added as well.
187
188         <form action="/path/to/login">
189           <input name="first_name" type="text">
190           <input type="submit" value="Ok">
191         </form>
192         <form action="/path/to/login.txt" method="POST">
193           <input name="first_name" type="text">
194           <input type="submit" value="Ok">
195         </form>
196         <form action="/path/to/login" enctype="multipart/form-data">
197           <input disabled="disabled" name="first_name" type="text">
198           <input type="submit" value="Ok">
199         </form>
200         <form action="http://example.com/login" method="POST">
201           <input name="first_name" type="text">
202           <input type="submit" value="Ok">
203         </form>
204         <form action="/path/to/delete/route?_method=DELETE" method="POST">
205           <input type="submit" value="Remove">
206         </form>
207
208   hidden_field
209         %= hidden_field foo => 'bar'
210         %= hidden_field foo => 'bar', id => 'bar'
211
212       Generate "input" tag of type "hidden".
213
214         <input name="foo" type="hidden" value="bar">
215         <input id="bar" name="foo" type="hidden" value="bar">
216
217   image
218         %= image '/images/foo.png'
219         %= image '/images/foo.png', alt => 'Foo'
220
221       Generate portable "img" tag.
222
223         <img src="/path/to/images/foo.png">
224         <img alt="Foo" src="/path/to/images/foo.png">
225
226   input_tag
227         %= input_tag 'first_name'
228         %= input_tag first_name => 'Default'
229         %= input_tag 'employed', type => 'checkbox'
230
231       Generate "input" tag. Previous input values will automatically get
232       picked up and shown as default.
233
234         <input name="first_name">
235         <input name="first_name" value="Default">
236         <input name="employed" type="checkbox">
237
238   javascript
239         %= javascript '/script.js'
240         %= javascript '/script.js', defer => undef
241         %= javascript begin
242           const a = 'b';
243         % end
244
245       Generate portable "script" tag for JavaScript asset.
246
247         <script src="/path/to/script.js"></script>
248         <script defer src="/path/to/script.js"></script>
249         <script><![CDATA[
250           const a = 'b';
251         ]]></script>
252
253   label_for
254         %= label_for first_name => 'First name'
255         %= label_for first_name => 'First name', class => 'user'
256         %= label_for first_name => begin
257           First name
258         % end
259         %= label_for first_name => (class => 'user') => begin
260           First name
261         % end
262
263       Generate "label" tag.
264
265         <label for="first_name">First name</label>
266         <label class="user" for="first_name">First name</label>
267         <label for="first_name">
268           First name
269         </label>
270         <label class="user" for="first_name">
271           First name
272         </label>
273
274   link_to
275         %= link_to Home => 'index'
276         %= link_to Home => 'index' => {format => 'txt'} => (class => 'menu')
277         %= link_to index => {format => 'txt'} => (class => 'menu') => begin
278           Home
279         % end
280         %= link_to Contact => 'mailto:sri@example.com'
281         <%= link_to index => begin %>Home<% end %>
282         <%= link_to '/file.txt' => begin %>File<% end %>
283         <%= link_to 'https://mojolicious.org' => begin %>Mojolicious<% end %>
284         <%= link_to url_for->query(foo => 'bar')->to_abs => begin %>Retry<% end %>
285
286       Generate portable "a" tag with "url_for" in Mojolicious::Controller,
287       defaults to using the capitalized link target as content.
288
289         <a href="/path/to/index">Home</a>
290         <a class="menu" href="/path/to/index.txt">Home</a>
291         <a class="menu" href="/path/to/index.txt">
292           Home
293         </a>
294         <a href="mailto:sri@example.com">Contact</a>
295         <a href="/path/to/index">Home</a>
296         <a href="/path/to/file.txt">File</a>
297         <a href="https://mojolicious.org">Mojolicious</a>
298         <a href="http://127.0.0.1:3000/current/path?foo=bar">Retry</a>
299
300       The first argument to "link_to" is the link content, except when the
301       final argument is Perl code such as a template block (created with the
302       "begin" and "end" keywords); in that case, the link content is omitted
303       at the start of the argument list, and the block will become the link
304       content.
305
306   month_field
307         %= month_field 'vacation'
308         %= month_field vacation => '2012-12'
309         %= month_field vacation => '2012-12', id => 'foo'
310
311       Generate "input" tag of type "month". Previous input values will
312       automatically get picked up and shown as default.
313
314         <input name="vacation" type="month">
315         <input name="vacation" type="month" value="2012-12">
316         <input id="foo" name="vacation" type="month" value="2012-12">
317
318   number_field
319         %= number_field 'age'
320         %= number_field age => 25
321         %= number_field age => 25, id => 'foo', min => 0, max => 200
322
323       Generate "input" tag of type "number". Previous input values will
324       automatically get picked up and shown as default.
325
326         <input name="age" type="number">
327         <input name="age" type="number" value="25">
328         <input id="foo" max="200" min="0" name="age" type="number" value="25">
329
330   password_field
331         %= password_field 'pass'
332         %= password_field 'pass', id => 'foo'
333
334       Generate "input" tag of type "password".
335
336         <input name="pass" type="password">
337         <input id="foo" name="pass" type="password">
338
339   radio_button
340         %= radio_button 'test'
341         %= radio_button country => 'germany'
342         %= radio_button country => 'germany', checked => undef, id => 'foo'
343
344       Generate "input" tag of type "radio". Previous input values will
345       automatically get picked up and shown as default.
346
347         <input name="test" type="radio">
348         <input name="country" type="radio" value="germany">
349         <input checked id="foo" name="country" type="radio" value="germany">
350
351   range_field
352         %= range_field 'age'
353         %= range_field age => 25
354         %= range_field age => 25, id => 'foo', min => 0, max => 200
355
356       Generate "input" tag of type "range". Previous input values will
357       automatically get picked up and shown as default.
358
359         <input name="age" type="range">
360         <input name="age" type="range" value="25">
361         <input id="foo" max="200" min="200" name="age" type="range" value="25">
362
363   search_field
364         %= search_field 'q'
365         %= search_field q => 'perl'
366         %= search_field q => 'perl', id => 'foo'
367
368       Generate "input" tag of type "search". Previous input values will
369       automatically get picked up and shown as default.
370
371         <input name="q" type="search">
372         <input name="q" type="search" value="perl">
373         <input id="foo" name="q" type="search" value="perl">
374
375   select_field
376         %= select_field country => ['de', 'en']
377         %= select_field country => [[Germany => 'de'], 'en'], id => 'eu'
378         %= select_field country => [[Germany => 'de', selected => 'selected'], 'en']
379         %= select_field country => [c(EU => [[Germany => 'de'], 'en'], id => 'eu')]
380         %= select_field country => [c(EU => ['de', 'en']), c(Asia => ['cn', 'jp'])]
381
382       Generate "select" and "option" tags from array references and
383       "optgroup" tags from Mojo::Collection objects.  Previous input values
384       will automatically get picked up and shown as default.
385
386         <select name="country">
387           <option value="de">de</option>
388           <option value="en">en</option>
389         </select>
390         <select id="eu" name="country">
391           <option value="de">Germany</option>
392           <option value="en">en</option>
393         </select>
394         <select name="country">
395           <option selected="selected" value="de">Germany</option>
396           <option value="en">en</option>
397         </select>
398         <select name="country">
399           <optgroup id="eu" label="EU">
400             <option value="de">Germany</option>
401             <option value="en">en</option>
402           </optgroup>
403         </select>
404         <select name="country">
405           <optgroup label="EU">
406             <option value="de">de</option>
407             <option value="en">en</option>
408           </optgroup>
409           <optgroup label="Asia">
410             <option value="cn">cn</option>
411             <option value="jp">jp</option>
412           </optgroup>
413         </select>
414
415   stylesheet
416         %= stylesheet '/foo.css'
417         %= stylesheet '/foo.css', title => 'Foo style'
418         %= stylesheet begin
419           body {color: #000}
420         % end
421
422       Generate portable "style" or "link" tag for CSS asset.
423
424         <link href="/path/to/foo.css" rel="stylesheet">
425         <link href="/path/to/foo.css" rel="stylesheet" title="Foo style">
426         <style><![CDATA[
427           body {color: #000}
428         ]]></style>
429
430   submit_button
431         %= submit_button
432         %= submit_button 'Ok!', id => 'foo'
433
434       Generate "input" tag of type "submit".
435
436         <input type="submit" value="Ok">
437         <input id="foo" type="submit" value="Ok!">
438
439   t
440         %= t div => 'test & 123'
441
442       Alias for "tag".
443
444         <div>test &amp; 123</div>
445
446   tag
447         %= tag 'br'
448         %= tag 'div'
449         %= tag 'div', id => 'foo', hidden => undef
450         %= tag 'div', 'test & 123'
451         %= tag 'div', id => 'foo', 'test & 123'
452         %= tag 'div', data => {my_id => 1, Name => 'test'}, 'test & 123'
453         %= tag div => begin
454           test & 123
455         % end
456         <%= tag div => (id => 'foo') => begin %>test & 123<% end %>
457
458       Alias for "new_tag" in Mojo::DOM.
459
460         <br>
461         <div></div>
462         <div id="foo" hidden></div>
463         <div>test &amp; 123</div>
464         <div id="foo">test &amp; 123</div>
465         <div data-my-id="1" data-name="test">test &amp; 123</div>
466         <div>
467           test & 123
468         </div>
469         <div id="foo">test & 123</div>
470
471       Very useful for reuse in more specific tag helpers.
472
473         my $output = $c->tag('meta');
474         my $output = $c->tag('meta', charset => 'UTF-8');
475         my $output = $c->tag('div', '<p>This will be escaped</p>');
476         my $output = $c->tag('div', sub { '<p>This will not be escaped</p>' });
477
478       Results are automatically wrapped in Mojo::ByteStream objects to
479       prevent accidental double escaping in "ep" templates.
480
481   tag_with_error
482         %= tag_with_error 'input', class => 'foo'
483
484       Same as "tag", but adds the class "field-with-error".
485
486         <input class="foo field-with-error">
487
488   tel_field
489         %= tel_field 'work'
490         %= tel_field work => '123456789'
491         %= tel_field work => '123456789', id => 'foo'
492
493       Generate "input" tag of type "tel". Previous input values will
494       automatically get picked up and shown as default.
495
496         <input name="work" type="tel">
497         <input name="work" type="tel" value="123456789">
498         <input id="foo" name="work" type="tel" value="123456789">
499
500   text_area
501         %= text_area 'story'
502         %= text_area 'story', cols => 40
503         %= text_area story => 'Default', cols => 40
504         %= text_area story => (cols => 40) => begin
505           Default
506         % end
507
508       Generate "textarea" tag. Previous input values will automatically get
509       picked up and shown as default.
510
511         <textarea name="story"></textarea>
512         <textarea cols="40" name="story"></textarea>
513         <textarea cols="40" name="story">Default</textarea>
514         <textarea cols="40" name="story">
515           Default
516         </textarea>
517
518   text_field
519         %= text_field 'first_name'
520         %= text_field first_name => 'Default'
521         %= text_field first_name => 'Default', class => 'user'
522
523       Generate "input" tag of type "text". Previous input values will
524       automatically get picked up and shown as default.
525
526         <input name="first_name" type="text">
527         <input name="first_name" type="text" value="Default">
528         <input class="user" name="first_name" type="text" value="Default">
529
530   time_field
531         %= time_field 'start'
532         %= time_field start => '23:59:59'
533         %= time_field start => '23:59:59', id => 'foo'
534
535       Generate "input" tag of type "time". Previous input values will
536       automatically get picked up and shown as default.
537
538         <input name="start" type="time">
539         <input name="start" type="time" value="23:59:59">
540         <input id="foo" name="start" type="time" value="23:59:59">
541
542   url_field
543         %= url_field 'address'
544         %= url_field address => 'https://mojolicious.org'
545         %= url_field address => 'https://mojolicious.org', id => 'foo'
546
547       Generate "input" tag of type "url". Previous input values will
548       automatically get picked up and shown as default.
549
550         <input name="address" type="url">
551         <input name="address" type="url" value="https://mojolicious.org">
552         <input id="foo" name="address" type="url" value="https://mojolicious.org">
553
554   week_field
555         %= week_field 'vacation'
556         %= week_field vacation => '2012-W17'
557         %= week_field vacation => '2012-W17', id => 'foo'
558
559       Generate "input" tag of type "week". Previous input values will
560       automatically get picked up and shown as default.
561
562         <input name="vacation" type="week">
563         <input name="vacation" type="week" value="2012-W17">
564         <input id="foo" name="vacation" type="week" value="2012-W17">
565

METHODS

567       Mojolicious::Plugin::TagHelpers inherits all methods from
568       Mojolicious::Plugin and implements the following new ones.
569
570   register
571         $plugin->register(Mojolicious->new);
572
573       Register helpers in Mojolicious application.
574

SEE ALSO

576       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
577
578
579
580perl v5.36.0                      2023-01-20Mojolicious::Plugin::TagHelpers(3)
Impressum