Last active
August 29, 2015 14:06
-
-
Save dkrnl/d0080bbea47b05239fbe to your computer and use it in GitHub Desktop.
Jinja macros for Bootstrap Forms
This file contains hidden or 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_open(form, action="", method="post", class="") -%} | |
<form role="form"{% if class %} class="{{ class }}"{% endif %}{% if action %} action="{{ action }}"{% endif %} method="{{ method|default("post") }}" {% if form.is_multipart() %}enctype="multipart/form-data" {% endif %}> | |
{%- for field in form.hidden_fields() -%} | |
{{ field|safe }} | |
{%- endfor -%} | |
{% endmacro %} | |
{% macro form_open_horizontal(form, action="", method="post") -%} | |
{{ form_open(form, action=action, method=method, class="form-horizontal") }} | |
{%- endmacro %} | |
{% macro form_close(form) -%} | |
{% csrf_token %} | |
</form> | |
{%- endmacro %} | |
{% macro non_field_errors(form) -%} | |
{% if form.non_field_errors %} | |
{%- for error in form.non_field_errors() -%} | |
<div class="alert alert-danger"> | |
<a class="close" data-dismiss="alert"><i class="fa fa-close"></i></a> | |
{{ error }} | |
</div> | |
{%- endfor -%} | |
{% endif %} | |
{%- endmacro %} | |
{% macro field_errors(field) %} | |
{%- if field.errors -%} | |
{%- if field|field_has_class("form-control") -%} | |
<span class="form-control-feedback"><i class="fa fa-exclamation-triangle"></i></span> | |
{% endif -%} | |
<span class="help-block"> | |
{%- for error in field.errors -%} | |
{{- error -}} | |
{%- if not loop.last %}<br />{% endif -%} | |
{%- endfor -%} | |
</span> | |
{%- endif -%} | |
{% endmacro %} | |
{% macro field_label(field, label_title="", label_class="") -%} | |
{% if field|field_widget_class != "checkboxinput" -%} | |
<label class="control-label{% if label_class %} {{ label_class }}{% endif %}"{% if field.auto_id %} for="{{ field.auto_id|safe }}"{% endif %}> | |
{{- "%s:"|format(label_title or field.label)|safe -}} | |
</label> | |
{%- endif %} | |
{%- endmacro %} | |
{% macro field_widget(field, input_prepend="", input_append="") -%} | |
{% if input_prepend or input_append %}<div class="input-group">{% endif -%} | |
{% if input_prepend %}<span class="input-group-addon">{{ input_prepend|safe }}</span>{% endif -%} | |
{% include ["planarium/forms/"+ field|field_widget_class + ".jinja", "planarium/forms/field.jinja"] %} | |
{%- if input_append %}<span class="input-group-addon">{{ input_append|safe }}</span>{% endif -%} | |
{%- if input_prepend or input_append %}</div>{% endif -%} | |
{%- endmacro %} | |
{% macro form_group(field, group_class="", label_title="", label_class="", field_class="", input_prepend="", input_append="") %} | |
{% with field_render = field_widget(field, input_prepend=input_prepend, input_append=input_append) %} | |
<div class="form-group{% if group_class %} {{ group_class }}{% endif %}{%- if field.errors %} has-error{% endif %}{%- if field|field_has_class('form-control') %} has-feedback{% endif %}"> | |
{{ field_label(field, label_title=label_title, label_class=label_class) }} | |
{% if field_class %}<div class="{{ field_class }}">{% endif %} | |
{{ field_render }} | |
{{ field_errors(field) }} | |
{% if field_class %}</div>{% endif %} | |
</div> | |
{% endwith %} | |
{%- endmacro %} | |
{% macro form_fields(form, ignore_fields=[], group_class="", label_class="", field_class="") %} | |
{% for field in form.visible_fields() %} | |
{% if field.name not in ignore_fields %}{{ form_group(field, group_class=group_class, label_class=label_class, field_class=field_class) }}{% endif %} | |
{%- endfor %} | |
{% endmacro %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment