All helpers automatically apply appropriate escaping. This means you can use
{{= ... }}
to output them. If you use {{h ... }}
et al., you will end up
double-escaping the output.
You can also address the helpers as methods on $this
in PHP template code.
Open a form like so:
{{= form ([ // (array) attributes
'method' => 'post',
'action' => '/hello',
]) }}
<form method="post" action="/hello" enctype="multipart/form-data">
You can close a form just using </form>
.
You can use a checkboxField
as a generic input field helper, but you will
have to set the checked
attribute yourself to mark it as checked or not.
Alternatively, if you specify the pseudo-attribute _options
, greater
functionality becomes available:
The _options
specify one or more checkboxes as part of the field,
with their value when checked, and their corresponding label.
If the _options
have more than one element, then field name will be
appended automatically with []
to make it an array.
The value
attribute will be matched against the _options
and the correct
checkboxes will be checked
for you.
The _default
pseudo-attribute, when present, will produce a hidden input
field for the value when no checkboxes are checked.
{{= checkboxField ([ // (array) attributes
'name' => 'flags',
'value' => 'bar',
'_default' => '',
'_options' => [
'foo' => 'Foo Flag',
'bar' => 'Bar Flag',
'baz' => 'Baz Flag',
]
]) }}
<input type="hidden" name="flags" value="" />
<label><input type="checkbox" name="flags[]" value="foo" /> Foo Flag</label>
<label><input type="checkbox" name="flags[]" value="bar" checked /> Bar Flag</label>
<label><input type="checkbox" name="flags[]" value="baz" /> Baz Flag</label>
{{= colorField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="color" name="foo" value="bar" />
{{= dateField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="date" name="foo" value="bar" />
{{= datetimeField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="datetime" name="foo" value="bar" />
{{= datetimeLocalField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="datetime-local" name="foo" value="bar" />
{{= emailField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="email" name="foo" value="bar" />
{{= fileField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="file" name="foo" value="bar" />
{{= hiddenField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="hidden" name="foo" value="bar" />
A generic input field; specify the type
needed.
{{= inputField ([ // (array) attributes
'type' => 'text',
'name' => 'foo',
'value' => 'bar',
// ...
]) }}
<input type="text" name="foo" value="bar" />
{{= monthField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="month" name="foo" value="bar" />
{{= numberField ([ // (array) attributes
'type' => 'number',
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="number" name="foo" value="bar" />
{{= passwordField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="password" name="foo" value="bar" />
You can use a radioField
as a generic input field helper, but you will
have to set the checked
attribute yourself to mark it as checked or not.
Alternatively, if you specify the pseudo-attribute _options
, greater
functionality becomes available:
The _options
specify one or more radio buttons as part of the field,
with their value when checked, and their corresponding label.
The value
attribute will be matched against the _options
and the correct
checkboxes will be checked
for you.
{{= radioField ([ // (array) attributes
'name' => 'foo',
'value' => 'baz',
'_options' => [
'bar' => 'Bar Label',
'baz' => 'Baz Label,
'dib' => 'Dib Label',
),
]) }}
<label><input type="radio" name="foo" value="bar" /> Bar Label</label>
<label><input type="radio" name="foo" value="baz" checked /> Baz Label</label>
<label><input type="radio" name="foo" value="dib" /> Dib Label</label>
{{= rangeField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="range" name="foo" value="bar" />
{{= searchField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="search" name="foo" value="bar" />
Use the pseudo-attribute _options
to describe the <option>
tags.
The attribute placeholder
is honored as a placeholder label when no
option is selected. The pseudo-attribute _default
specifies the value of
the placeholder.
Using the attribute 'multiple' => true
will set up a multiple select, and
automatically add []
to the name if it is not already there.
{{= select ([ // (array) attributes
'name' => 'foo',
'value' => 'dib',
'placeholder' => 'Please pick one',
'_default' => '',
'_options' => [
'bar' => 'Bar Label',
'baz' => 'Baz Label',
'dib' => 'Dib Label',
'zim' => 'Zim Label',
],
]) }}
<select name="foo">
<option value="" disabled>Please pick one</option>
<option value="bar">Bar Label</option>
<option value="baz">Baz Label</option>
<option value="dib" selected>Dib Label</option>
<option value="zim">Zim Label</option>
</select>
The helper also supports option groups. If an _options
array value is itself
an array, the key for that element will be used as an <optgroup>
label and
the array of values will be options under that group.
{{= select ([
'name' => 'foo',
'value' => 'bar',
'_options' => [
'Group A' => [
'bar' => 'Bar Label',
'baz' => 'Baz Label',
],
'Group B' => [
'dib' => 'Dib Label',
'zim' => 'Zim Label',
],
],
]) }}
<select name="foo">
<optgroup label="Group A">
<option value="bar">Bar Label</option>
<option value="baz">Baz Label</option>
</optgroup>
<optgroup label="Group B">
<option value="dib" selected>Dib Label</option>
<option value="zim">Zim Label</option>
</optgroup>
</select>
{{= telField([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="tel" name="foo" value="bar" />
{{= textField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="text" name="foo" value="bar" />
{{= textarea ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<textarea name="foo">bar</textarea>
{{= timeField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="time" name="foo" value="bar" />
{{= urlField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="url" name="foo" value="bar" />
{{= weekField ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="week" name="foo" value="bar" />
Helpers for various button tags.
{{= button ([ // (array) atttributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="button" name="foo" value="bar" />
{{= imageButton ([ // (array) atttributes
'name' => 'foo',
'src' => '/images/map.png',
]) }}
<input type="image" name="foo" src="/images/map.png" />
{{= submitButton ([ // (array) atttributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="submit" name="foo" value="bar" />
{{= resetButton ([ // (array) attributes
'name' => 'foo',
'value' => 'bar',
]) }}
<input type="reset" name="foo" value="bar" />
A helper for <label>
tags.
{{= label (
'Label For Field', // (string) label text
[ // (array) optional attributes
'for' => 'field'
]
) }}
<label for="field">Label For Field</label>