Skip to content

Instantly share code, notes, and snippets.

@maximebf
Created October 31, 2012 11:56
Show Gist options
  • Save maximebf/3986659 to your computer and use it in GitHub Desktop.
Save maximebf/3986659 to your computer and use it in GitHub Desktop.

Revisions

  1. maximebf created this gist Oct 31, 2012.
    39 changes: 39 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    {% macro form_field(field) -%}
    {% set with_label = kwargs.pop('with_label', False) %}
    {% set placeholder = '' %}
    {% if not with_label %}
    {% set placeholder = field.label.text %}
    {% endif %}
    <div class="control-group {% if field.errors %}error{% endif %}">
    {% if with_label %}
    <label for="{{ field.id }}" class="control-label">
    {{ field.label.text }}{% if field.flags.required %} *{% endif %}:
    </label>
    {% endif %}
    <div class="controls">
    {% set class_ = kwargs.pop('class_', '') %}
    {% if field.flags.required %}
    {% set class_ = class_ + ' required' %}
    {% endif %}
    {% if field.type == 'BooleanField' %}
    <label class="checkbox">
    {{ field(class_=class_, **kwargs) }}
    {{ field.label.text|safe }}
    </label>
    {% else %}
    {% if field.type in ('TextField', 'TextAreaField', 'PasswordField') %}
    {% set class_ = class_ + ' input-xlarge' %}
    {% elif field.type == 'FileField' %}
    {% set class_ = class_ + ' input-file' %}
    {% endif %}
    {{ field(class_=class_, placeholder=placeholder, **kwargs) }}
    {% endif %}
    {% if field.errors %}
    <span class="error help-inline">{{ field.errors|join(', ') }}</span>
    {% endif %}
    {% if field.description %}
    <p class="help-block">{{ field.description|safe }}</p>
    {% endif %}
    </div>
    </div>
    {%- endmacro %}