Last active
January 3, 2016 01:29
-
-
Save deguchi/8389032 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%- macro form_field_label(field, label_width) -%} | |
<label for="id_{{field.name}}" class="col-sm-{{label_width}} control-label">{{field.label}}:{%- if field.field.required -%} | |
<abbr title="この項目は必須です" class="label label-warning">必須</abbr> | |
{%- endif %}</label> | |
{% endmacro %} | |
{%- macro form_field_description(field) -%} | |
{% if field.help_text %} | |
<span class="help-block">{{ field.help_text }}</span> | |
{% endif %} | |
{%- endmacro -%} | |
{%- macro form_field_errors(field) -%} | |
{% if field.errors %} | |
<div style="margin-bottom:15px;margin-top:10px"> | |
{%- for error in field.errors -%} | |
<div> | |
<span class="label label-danger"><i class="icon-exclamation-sign icon-white"></i> {{ error|safe }}</span> | |
</div> | |
{%- endfor -%} | |
</div> | |
{% endif %} | |
{%- endmacro -%} | |
{%- macro form_field_file(field) -%} | |
<div class="fileupload fileupload-new" data-provides="fileupload"> | |
<div class="input-append"> | |
<div class="uneditable-input span3"><i class="icon-file fileupload-exists"></i> <span class="fileupload-preview"></span></div><span class="btn btn-file"><span class="fileupload-new">ファイルを選択</span><span class="fileupload-exists">変更</span><input type="file" name="{{field.name}}" /></span><a href="#" class="btn fileupload-exists" data-dismiss="fileupload">選択解除</a> | |
</div> | |
</div> | |
{%- endmacro -%} | |
{%- macro action_buttons(label_width, input_width, submit_title="作成", cancel_title="キャンセル", submit_class="btn-primary") -%} | |
<div class="form-group"> | |
<div class="col-sm-offset-{{label_width}} col-sm-{{input_width}}"> | |
<input type="submit" class="btn btn-lg {{submit_class}}" value="{{submit_title}}" /> | |
</div> | |
| |
<button type="reset" class="btn">{{cancel_title}}</button> | |
</div> | |
{%- endmacro -%} | |
{%- macro form_field(field, label_width, input_width) -%} | |
<div class="form-group{% if field.errors %} has-error{% endif %}"> | |
{% if field.is_hidden %} | |
{{ field() }} | |
{% else %} | |
{{ form_field_label(field, label_width) }} | |
<div class="col-sm-{{input_width}}"> | |
{% if field.field.widget.__class__.__name__ == 'Textarea' %} | |
{{ field|addattr({'class':'form-control', 'row':5}) }} | |
{% elif field.field.widget.__class__.__name__ == 'Radio' %} | |
{{ field() }} | |
{% elif field.field.widget.__class__.__name__ == 'File' %} | |
{{ form_field_file(field) }} | |
{% else %} | |
{{ field|addattr({'class':'form-control'})|safe }} | |
{% endif %} | |
{{ form_field_errors(field) }} | |
{{ form_field_description(field) }} | |
</div> | |
{% endif %} | |
</div> | |
{%- endmacro -%} | |
{%- macro form_fields(fields, class=None, legend=None) -%} | |
<fieldset {% if class %}class="{{class}}"{% endif %}> | |
{% if legend %} | |
<legend>{{legend}}</legend> | |
{% endif %} | |
{% for field in fields %} | |
{% if field.type == 'HiddenField' %} | |
{{ field() }} | |
{% else %} | |
{{ form_field(field) }} | |
{% endif %} | |
{% endfor %} | |
</fieldset> | |
{%- endmacro -%} | |
{# How to use #} | |
{% from "macro/form.jinja" import form_field %} | |
<form method="post" action="/form/create" class="form-horizontal" role="form"> | |
{% csrf_token %} | |
{% for field in form %} | |
{{form_field(field, 2, 10)}} | |
{% endfor %} | |
{% from "macro/form.jinja" import action_buttons %} | |
{% if action=='create' %} | |
{{action_buttons(2, 10, 'Create')}} | |
{% else %} | |
{{action_buttons(2, 10, 'Save')}} | |
{% endif %} | |
</form> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment