macros.html 3.26 KB
Newer Older
YSelf Tool's avatar
YSelf Tool committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

{% macro render_field(field, label_visible=true) -%}
    {{ field(id=field.id, title=field.description, **kwargs) }}
    {% if field.errors %}
        {% for e in field.errors %}
            <p class="help-block">{{ e }}</p>
        {% endfor %}
    {% endif %}
{%- endmacro %}

{% macro render_stringfield(field) -%}
    <div class="mdl-textfield mdl-js-textfield">
        <input id="{{ field.id }}" name="{{ field.id }}" class="mdl-textfield__input" type="text" />
        <label class="mdl-textfield__label" for="{{ field.id }}">{{ field.label.text }}</label>
        {% if field.errors %}
            {% for e in errors %}
                <div class="mdl-card__supporting-text">
                    {{ e }}
                </div>
            {% endfor %}
        {% endif %}
    </div>
{%- endmacro %}

{% macro render_passwordfield(field) -%}
    <div class="mdl-textfield mdl-js-textfield">
        <input id="{{ field.id }}" name="{{ field.id }}" class="mdl-textfield__input" type="password" />
        <label class="mdl-textfield__label" for="{{ field.id }}">{{ field.label.text }}</label>
        {% if field.errors %}
            {% for e in errors %}
                <div class="mdl-card__supporting-text">
                    {{ e }}
                </div>
            {% endfor %}
        {% endif %}
    </div>
{%- endmacro %}

{% macro render_booleanfield(field) -%}
    <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="{{ field.id }}">
        <input type="checkbox" id="{{ field.id }}" name="{{ field.id }}" class="mdl-checkbox__input" />
        <span class="mdl-checkbox__label">{{ field.label.text }}</span>
    </label>
{%- endmacro %}

{% macro render_csrftokenfield(field, kwargs) -%}
    {{ field(title=field.description, **kwargs) }}
{%- endmacro %}

{% macro render_form(form, action_url="", title=None, action_text="Submit", class_="mdl-card mdl-shadow--2dp", title_class="mdl-card__title", title_next_class="mdl-card__title-text", content_class="mdl-card__supporting-text", action_class="mdl-card__actions", btn_class="mdl-button mdl-js-button mdl-button--raised mdl-button-colored") -%}
    <div class="{{ class_ }}">
        <form method="POST" action="{{ action_url }}">
            <div class="{{ title_class }}">
                <h2 class="{{ title_text_class}}">{{ title if title is not none else action_text }}</h2>
            </div>
            <div class="{{ content_class }}">
                {% for f in form %}
                    {% if f.type == "StringField" %}
                        {{ render_stringfield(f) }}
                    {% elif f.type == "PasswordField" %}
                        {{ render_passwordfield(f) }}
                    {% elif f.type == "BooleanField" %}
                        {{ render_booleanfield(f) }}
                    {% elif f.type == "CSRFTokenField" %}
                        {{ render_csrftokenfield(f, kwargs) }}
                    {% else %}
                        {{ f.type }}
                        {{ render_field(f) }}
                    {% endif %}
                {% endfor %}
                {% endfor %}
            </div>
            <div class="{{ action_class }}">
                <button type="submit" class="{{ btn_class }}">{{ action_text }}</button>
            </div>
        </form>
    </div>
{%- endmacro %}