Checked/Selected
checked, selected
Description
{{ checked key=$optionValue value=$currentValue }}
{{ selected key=$optionValue value=$currentValue }}
{{ checked key=1 value=$currentValue typecheck=true }}
Prints checked (or selected, with a leading space) when the current value matches the value of this input or option:
keyis the value of this<input>/<option>: a variable, a number or a quoted string.valueis the current value, usually from the request ($in.category) or the stored data ($page.data[$id]).- It matches when
value == key, or whenvalueis an array that containskey(multiple checkboxes, multiple select). - Add
typecheck=trueto compare with===(and a strictin_array). - Nothing is printed when
valueis not set.
Put the tag directly after the other attributes, without a space before it: the output starts with its own space.
Example
Controller
$this->page['current'] = 'value';
Template
<label><input type="radio" name="radio" value="value"{{ checked key='value' value=$page.current }}> value</label>
<label><input type="radio" name="radio" value="other"{{ checked key='other' value=$page.current }}> other</label>
<select name="select">
<option value="value"{{ selected key='value' value=$page.current }}>value</option>
<option value="other"{{ selected key='other' value=$page.current }}>other</option>
</select>
Output
<label><input type="radio" name="radio" value="value" checked> value</label>
<label><input type="radio" name="radio" value="other"> other</label>
<select name="select">
<option value="value" selected>value</option>
<option value="other">other</option>
</select>
Multiple checkboxes
Controller
$this->page['data']['colors'] = array('red', 'blue');
$this->page['colors'] = array('red', 'green', 'blue');
Template
{{ foreach $page.colors as $color }}
<label><input type="checkbox" name="colors[]" value="{{ $color }}"{{ checked key=$color value=$page.data.colors }}> {{ $color }}</label>
{{ end foreach }}
Output
<label><input type="checkbox" name="colors[]" value="red" checked> red</label>
<label><input type="checkbox" name="colors[]" value="green"> green</label>
<label><input type="checkbox" name="colors[]" value="blue" checked> blue</label>
See Options to build all the <option> tags of a <select> from an array.