Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created January 14, 2013 16:42
Show Gist options
  • Select an option

  • Save doobeh/4531399 to your computer and use it in GitHub Desktop.

Select an option

Save doobeh/4531399 to your computer and use it in GitHub Desktop.
Jinja2 Bootstrap Form Rendering Macro
{% macro render_field(field,help=False,icon=False,counter=False,text=False) %}
{% if field.type not in ('SubmitField') %}
<div class="control-group{% if field.errors %} warning{% endif %}">
{% if field.type in ('EmailField','PasswordField','TextField','TextAreaField','FileField',
'QuerySelectFieldCustom','DecimalField','SelectField','BooleanField','IntegerField','DecimalField'
) %}
{{ field.label(class="control-label") }}
<div class="controls">
{% if icon %}
<div class="input-prepend">
<span class="add-on"><i class="{{ icon }}"></i></span>
{% endif %}
{% if text %}
<div class="input-prepend">
<span class="add-on">{{ text }}</span>
{% endif %}
{{ field(**kwargs)|safe }}
{% if icon or text%}</div>{% endif %}
{# Show Errors #}
{% if field.errors %}
<p class="help-block">
{%- for error in field.errors %}<span class="label label-warning"><i class="icon-exclamation-sign icon-white"></i> {{ error }}</span><br/>{% endfor -%}
</p>
{% endif %}
{% if help or counter%}
<p class="help-block">{{ help|safe }}{% if counter %} <span class="counter-display"></span> characters left. {% endif %}</p>
{% endif %}
</div>
{% elif field.type in ('MultiQuerySelectField') %}
{% for subfield in field %}
<div class="row-fluid">
<div class="span6 toggle-label">
<h1 class="pull-right">{{ subfield.label }}</h1>
</div>
<div class=" toggle">
{{ subfield(**kwargs)|safe }}
</div>
</div>
{% endfor %}
{% elif field.type in ('RadioField') %}
{{ field.label(class="control-label") }}
<div class="controls">
{% for subfield in field %}
<label class="radio">
{{ subfield(**kwargs)|safe }}
{{ subfield.label.text }}
</label>
{% endfor %}
{% if help %}
{% if field.errors %}
<p class="help-block">
{%- for error in field.errors %}<i class="icon-star"></i> {{ error }}{% endfor -%}
</p>
{% endif %}
<p class="help-block">{{ help }}</p>
{% endif %}
</div>
{% else %}
{{ field(**kwargs)|safe }}
{% endif %}
</div>
{% else %}
{{ field(**kwargs)|safe }}
{% endif %}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment