メインコンテンツまでスキップ
Version: Fegg 5.x

チェック/選択

checked, selected

説明​

{{ checked key=$optionValue value=$currentValue }}
{{ selected key=$optionValue value=$currentValue }}
{{ checked key=1 value=$currentValue typecheck=true }}

現在の値が、この入力や選択肢の値と一致するときに、 checked(または selected。先頭にスペースが付きます)を出力します。

  • keyは、この<input>/<option>の値です。変数、数値、引用符で囲んだ文字列で書きます。
  • valueは現在の値です。通常はリクエスト($in.category)か、保存されたデータ($page.data[$id])から取ります。
  • value == keyのとき、またはvalueがkeyを含む配列のとき(複数のチェックボックス、複数選択)に一致とみなします。
  • typecheck=trueを付けると、===で比較します(in_arrayも厳密な比較になります)。
  • valueが設定されていなければ、何も出力しません。

タグは、ほかの属性の直後に、前にスペースを入れずに書きます。出力の先頭にスペースが付くためです。

例​

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>

複数のチェックボックス​

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>

<select>の<option>タグを配列からまとめて作るには、オプションを参照してください。