List of Mojolicious "tag helpers"

A list of Mojolicious tag helpers.

Text field

The text_field tag helper makes it easy to write text fields .

<%= text_field'first_name'%>
<%= text_field first_name =>'Default name'%>
<%= text_field first_name =>'Default name', class =>'user'%>

The first argument is the value of name, the second argument is the value of value, and the remaining arguments are used as attributes. It will be expanded as follows.

<input name = "first_name" />
<input name = "first_name" value = "Default name" />
<input class = "user" name = "first_name" value = "Default name" />

Password field

The password_field tag helper makes it easy to write password fields .

<%= password_field'pass'%>
<%= password_field'pass', id =>'foo'%>

The first argument is the value of name and the remaining arguments are used as attributes.

<input name = "pass" type = "password" />
<input id = "foo" name = "pass" type = "password" />

Text area

The text_area tag helper makes it easy to write text areas .

<%= text_area'foo'%>
<%= text_area foo => begin%>
  Default!
<%end%>

The first argument is the value of name. You can display the contents of the text area by passing a block (begin to end) as the second argument. It will be expanded as follows.

  <textarea name = "foo"> </textarea>
  <textarea name = "foo">
    Default!
  </textarea>

Checkbox

The check_box tag helper makes it easy to write checkboxes. Even when the page is changed, the checkbox is automatically checked if the parameter is set to a value.

<%= check_box employed => 1%> # name => value
<%= check_box employed => 1, id =>'foo'%>

It will be expanded as follows. The first argument is the value of name and the second argument is the value of value. Attributes for the remaining arguments

Can be described.

<input name = "employed" type = "checkbox" value = "1" />
<input id = "foo" name = "employed" type = "checkbox" value = "1" />

Radio button

The radio_button tag helper makes it easy to write radio buttons .

<%= radio_button country =>'germany'%>
<%= radio_button country =>'germany', id =>'foo'%>

The first argument is the value of name, the second argument is the value of value, and the remaining arguments are used as attributes. If the value corresponding to the parameter of the request is set, it will be checked automatically.

<input name = "country" type = "radio" value = "germany" />
<input id = "foo" name = "country" type = "radio" value = "germany" />

Select field

The select_field tag helper makes it easy to write select fields . Select fields are used to create combo boxes (drop-down menus) and list boxes.

<%= select_field language => [qw / de en /]%>
<%= select_field language => [qw / de en /], id =>'lang'%>
<%= select_field country => [[Germany =>'de'],'en']%>
<%= select_field country => [{Europe => [[Germany =>'de'],'en']}]%>
<%= select_field country => [[Germany =>'de', class =>'europe'],'en']%>

The first argument is the value of name. The second argument is the value of name and value of the option tag to be placed in the select tag. You can write various things using hash references and array references. The value of the parameter selected in the request is automatically selected. It will be expanded as follows.

<select name = "language">
  <option value = "de"> de </option>
  <option value = "en"> en </option>
</select>
<select id = "lang" name = "language">
  <option value = "de"> de </option>
  <option value = "en"> en </option>
</select>
<select name = "country">
  <option value = "de"> Germany </option>
  <option value = "en"> en </option>
</select>
<select id = "lang" name = "language">
  <optgroup label = "Europe">
    <option value = "de"> Germany </option>
    <option value = "en"> en </option>
  </optgroup>
</select>
<select name = "country">
  <option class = "europe" value = "de"> Germany </option>
  <option value = "en"> en </option>
</select>

Describe the file field

The file_field tag helper makes it easy to write file fields.

<%= file_field'avatar'%>
<%= file_field'avatar', id =>'foo'%>

It will be expanded as follows. The first argument is the value of name. The remaining arguments are the name and value of the attribute.

<input name = "avatar" type = "file" />
<input id = "foo" name = "avatar" type = "file" />

Describe hidden fields

The hidden_field tag helper makes it easy to write hidden fields .

<%= hidden_field foo =>'bar'%>
<%= hidden_field foo =>'bar', id =>'bar'%>

It expands to tags like the following: The first argument is the value of name, the second argument is the value of value, and the remaining arguments are used as attributes.

<input name = "foo" type = "hidden" value = "bar" />
<input id = "bar" name = "foo" type = "hidden" value = "bar" />

Write a submit button

The submit_button tag helper makes it easy to write a submit button.

<%= submit_button%>
<%= submit_button'Ok!', Id =>'foo'%>

The first argument is the value of value. This value is displayed as suitable for the button. The remaining arguments are used as attributes.

<input type = "submit" value = "Ok" />
<input id = "foo" type = "submit" value = "Ok!" />

Hyperlink

The link_to tag helper makes it easy to write hyperlinks .

<%= link_to index => begin%> Home <%end%>
<%= link_to index => {foo =>'bar'} => (class =>'links') => begin%>
  Home
<%end%>
<%= link_to'/ path / to / file' => begin%> File <%end%>
<%= link_to'http://mojolicio.us' => begin%> Mojolicious <%end%>
<%= link_to url_for->query(foo => $foo) => begin%> Retry <%end%>

It will be expanded to the following HTML. The first argument is the root name or path. You can also pass a Mojo::URL object. The last argument is a block, which allows you to specify the text of the hyperlink.

<a href="/path/to/index"> Home</a>
<a class="links" href="/path/to/index/bar"> Home</a>
<a href="/path/to/file"> File</a>
<a href="http://mojolicio.us"> Mojolicious</a>
<a href="/current/path?foo=something"> Retry</a>

Describe image tags

The image tag helper makes it easy to write image tags .

<%= image'/images/foo.png'%>
<%= image'/images/foo.png', alt =>'Foo'%>

It will be expanded as follows. The first argument is the URL to the image. This URL is expanded by the url_for method. The remaining arguments are used as attributes.

<img src = "/ images / foo.png" />
<img alt = "Foo" src = "/ images / foo.png" />

Style Sheet (CSS)

The stylesheet tag helper makes it easy to write external links and stylesheets to stylesheets.

<%= stylesheet'/foo.css'%>
<%= stylesheet begin%>
  body {color: # 000}
<%end%>

When specifying the link to the external style sheet, describe the path in the first argument. If you want to describe the style sheet itself, specify the block (begin to end) as the first argument. It will be expanded to the following HTML.

<link href = "/ foo.css" media = "screen" rel = "stylesheet" type = "text / css" />
<style type = "text / css"> <! [CDATA [
  body {color: # 000}
]]> </style>

JavaScript source code

The javascript tag helper makes it easy to write JavaScript source code.

%= javascript'/script.js';
%= javascript begin
  var a ='b';
%end

The argument can be an external JavaScript path or block (begin to end). You can write the actual JavaScript source code inside the block.

<script src = "/ script.js" type = "text / javascript" />
<script type = "text / javascript"> <! [CDATA [
  var a ='b';
]]> </script>

Input tag

The input_tag tag helper makes it easy to write input tags .

<%= input_tag'first_name'%>
<%= input_tag first_name =>'Default name'%>
<%= input_tag'employed', type =>'checkbox'%>
<%= input_tag'country', type =>'radio', value =>'germany'%>

It will be expanded as follows. The first argument is the value of name and the remaining arguments are used as attributes. Input values ​​contained in HTTP request parameters are automatically completed.

<input name = "first_name" />
<input name = "first_name" value = "Default name" />
<input name = "employed" type = "checkbox" />
<input name = "country" type = "radio" value = "germany" />

Base tag

You can easily write a base tag using the base_tag function, which is a tag helper.

<%= base_tag%>

It will be expanded to the following tags.

<base href = "http: //localhost/cgi-bin/myapp.pl" />

Associated Information