LoginSignup
14
14

More than 5 years have passed since last update.

Phalcon の Tag のヘルパによる表示例

Last updated at Posted at 2014-07-22

Phalcon\Tag にはいろいろヘルパがあるがここではフォームに関するものについて volt での書き方と html の出力結果を書いておく.

基本

ここでは text_field() を使って例を出す。

  • 何も設定しない
volt
{{ text_field() }}
html
<input type="text" />
  • 第一引き数に文字列を与える
volt
{{ text_field('hoge') }}
html
<input type="text" id="hoge" name="hoge" />
  • 第二引き数以降にキーと値を与える
volt
{{ text_field('hoge', 'class':'myclass', 'aaa': 'bbb') }}
html
<input type="text" id="hoge" name="hoge" class="myclass" aaa="bbb" />

出力例

text_field

「基本」を参照.

password_field

volt
{{ password_field('hoge') }}
html
<input type="password" id="hoge" name="hoge" />

file_field

volt
{{ file_field('hoge') }}
html
<input type="file" id="hoge" name="hoge" />

check_field

volt
{{ check_field('hoge') }}
html
<input type="checkbox" id="hoge" name="hoge" />

radio_field

volt
{{ radio_field('hoge') }}
html
<input type="radio" id="hoge" name="hoge" />

date_field

volt
{{ date_field('hoge') }}
html
<input type="date" id="hoge" name="hoge" />

email_field

volt
{{ email_field('hoge') }}
html
<input type="email" id="hoge" name="hoge" />

numeric_field

screenshot 2014-07-23 4.17.17.png

ドキュメントが間違っているようだけど number_field ではなく numeric_field とのこと.
ドキュメント直ってないけどなぜか下記 Issue は close されている.

Phalcon\Tag のページだと numericField になっている.

screenshot 2014-07-23 4.18.59.png

volt
{{ numeric_field('hoge') }}
html
<input type="number" id="hoge" name="hoge" />

submit_button

volt
{{ submit_button() }}
html
<input type="submit" />

select_static

このヘルパは 第一引き数だけだとエラーとなって動かない

volt
{{ select_static('hoge') }}
html
Invalid data provided to SELECT helper

第二引き数に連想配列を指定

volt
{{ select_static('hoge', ['A': 'Active', 'B': 'Inactive']) }}
html
<select id="hoge" name="hoge">
    <option value="A">Active</option>
    <option value="B">Inactive</option>
</select>

multiple を付けたい場合は単体だと表示してくれないので空文字列でも設定しておく.

volt
{{ select_static('hoge', ['A': 'Active', 'B': 'Inactive'], 'multiple': '') }}
html
<select id="hoge" name="hoge" multiple="">
    <option value="A">Active</option>
    <option value="B">Inactive</option>
</select>

表示例

screenshot 2014-07-23 4.24.39.png

select

select_static と同様,第一引き数だけだとエラーになる.

volt
{{ select('hoge') }}
html
Invalid data provided to SELECT helper

select と select_static の違いは select は Phalcon\Mvc\Model\ResultsetInterface を渡すことができるらしい.
普通に連想配列を渡すと select_static と同じ動きはする. ResultsetInterface のほうはここでは省略.

volt
{{ select('hoge', ['A': 'Active', 'B': 'Inactive']) }}
html
<select id="hoge" name="hoge">
    <option value="A">Active</option>
    <option value="B">Inactive</option>
</select>

text_area

volt
{{ text_area('hoge') }}
html
<textarea id="hoge" name="hoge"></textarea>

中身を追加したい場合.なぜか value 属性がついてて要らない気もするけど.

volt
{{ text_area('hoge', 'value': 'This is contents.') }}
html
<textarea id="hoge" name="hoge" value="This is contents.">This is contents.</textarea>

form

volt
{{ form() }}
html
<form method="post">

第一引き数を指定した場合

volt
{{ form('hoge') }}
html
<form action="/hoge" method="post">

属性指定は他と同様.

volt
{{ form('hoge', 'enctype': 'multipart/form-data') }}
html
<form action="/hoge" enctype="multipart/form-data" method="post">

end_form

volt
{{ end_form() }}
html
</form>

ちなみに文字列で引き数渡しても効果はない

volt
{{ end_form('hoge') }}
html
</form>

参考文献

API ドキュメント

Class Phalcon\Tag — Phalcon 1.3.1 documentation
http://docs.phalconphp.com/ja/latest/api/Phalcon_Tag.html

Volt についての説明

Volt: テンプレートエンジン — Phalcon 1.3.1 documentation
http://docs.phalconphp.com/ja/latest/reference/volt.html

ヘルパの説明

Viewヘルパ — Phalcon 1.3.1 documentation
http://docs.phalconphp.com/ja/latest/reference/tags.html

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14