macros.html 4.39 KB
Newer Older
YSelf Tool's avatar
YSelf Tool committed
1 2 3 4 5 6 7 8 9 10 11

{% 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) -%}
YSelf Tool's avatar
YSelf Tool committed
12 13
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
        <input id="{{ field.id }}" name="{{ field.id }}" class="mdl-textfield__input" type="text" value="{{ field.data }}" />
YSelf Tool's avatar
YSelf Tool committed
14 15 16 17 18 19 20 21 22 23 24 25
        <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) -%}
YSelf Tool's avatar
YSelf Tool committed
26
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
YSelf Tool's avatar
YSelf Tool committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        <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>
YSelf Tool's avatar
YSelf Tool committed
44 45 46 47 48 49 50
    {% if field.errors %}
        {% for e in errors %}
            <div class="mdl-card__supporting-text">
                {{ e }}
            </div>
        {% endfor %}
    {% endif %}
YSelf Tool's avatar
YSelf Tool committed
51 52 53 54 55 56
{%- endmacro %}

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

YSelf Tool's avatar
YSelf Tool committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
{% macro render_selectmultiplefield(field) -%}
        <button id="{{ field.id }}-button" class="mdl-button mdl-js-button" type="button">
            {{ field.label.text }}
        </button>
        <select id="{{ field.id }}" name="{{ field.id }}" multiple class="mdl-menu mdl-js-menu mdl-menu--top-left" for="{{ field.id }}-button">
            {% for name, text in field.choices %}
                <option value="{{ name }}" {% if field.data is not none and name in field.data %}selected{% endif %} class="mdl-menu__item" >{{ text }}</option>
            {% endfor %}
        </select>
        {% if field.errors %}
            {% for e in field.errors %}
                <p class="help-block">{{ e }}</p>
            {% endfor %}
        {% endif %}
    </label>
{%- endmacro %}



YSelf Tool's avatar
YSelf Tool committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{% 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) }}
YSelf Tool's avatar
YSelf Tool committed
92 93
                    {% elif f.type == "SelectMultipleField" %}
                        {{ render_selectmultiplefield(f) }}
YSelf Tool's avatar
YSelf Tool committed
94 95 96 97 98 99 100 101 102 103 104 105
                    {% else %}
                        {{ f.type }}
                        {{ render_field(f) }}
                    {% endif %}
                {% endfor %}
            </div>
            <div class="{{ action_class }}">
                <button type="submit" class="{{ btn_class }}">{{ action_text }}</button>
            </div>
        </form>
    </div>
{%- endmacro %}