macros.html 6.24 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
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
13
        <input id="{{ field.id }}" name="{{ field.id }}" class="mdl-textfield__input" type="text" {% if field.data is not none %}value="{{ field.data }}"{% endif %} />
YSelf Tool's avatar
YSelf Tool committed
14 15
        <label class="mdl-textfield__label" for="{{ field.id }}">{{ field.label.text }}</label>
        {% if field.errors %}
YSelf Tool's avatar
YSelf Tool committed
16
            {% for e in field.errors %}
YSelf Tool's avatar
YSelf Tool committed
17 18 19 20 21 22 23 24 25
                <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
        <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 %}
YSelf Tool's avatar
YSelf Tool committed
30
            {% for e in field.errors %}
YSelf Tool's avatar
YSelf Tool committed
31 32 33 34 35 36 37 38 39 40 41 42 43
                <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
    {% if field.errors %}
YSelf Tool's avatar
YSelf Tool committed
45
        {% for e in field.errors %}
YSelf Tool's avatar
YSelf Tool committed
46 47 48 49 50
            <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
{% macro render_selectmultiplefield(field) -%}
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    <button id="{{ field.id }}-button" class="mdl-button mdl-js-button mdl-button--colored mdl-shadow--2dp mdl-color--grey-100 " 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 %}
            <div class="mdl-card__supporting-text">
                {{ e }}
            </div>
        {% endfor %}
    {% endif %}
{%- endmacro %}

{% macro render_datefield(field) -%}
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
        <input id="{{ field.id }}" name="{{ field.id }}" class="mdl-textfield__input" type="text"{% if field.data is not none %} value="{{ field.data.strftime("%d.%m.%Y") }}"{% endif %} />
        <label class="mdl-textfield__label" for="{{ field.id }}">{{ field.label.text }}</label>
YSelf Tool's avatar
YSelf Tool committed
85 86
        {% if field.errors %}
            {% for e in field.errors %}
YSelf Tool's avatar
YSelf Tool committed
87 88 89
                <div class="mdl-card__supporting-text">
                    {{ e }}
                </div>
YSelf Tool's avatar
YSelf Tool committed
90 91
            {% endfor %}
        {% endif %}
92
    </div>
YSelf Tool's avatar
YSelf Tool committed
93 94
{%- endmacro %}

95 96 97 98 99 100 101 102 103 104
{% macro render_hiddenfield(field) -%}
    <input id="{{ field.id }}" name="{{ field.id }}" type="hidden"{% if field.data is not none %} value="{{ field.data }}"{% endif %} />
    {% if field.errors %}
        {% for e in field.errors %}
            <div class="mdl-card__supporting-text">
                {{ e }}
            </div>
        {% endfor %}
    {% endif %}
{%- endmacro %}
YSelf Tool's avatar
YSelf Tool committed
105 106


107
{% macro render_form(form, action_url="", title=None, action_text="Submit", class_="mdl-card mdl-shadow--2dp mdl-cell mdl-cell--4-col mdl-cell--4-col-tablet mdl-cell--4-col-phone", title_class="mdl-card__title", title_text_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", btn_value="", btn_list=None) -%}
YSelf Tool's avatar
YSelf Tool committed
108 109 110
    <div class="{{ class_ }}">
        <form method="POST" action="{{ action_url }}">
            <div class="{{ title_class }}">
YSelf Tool's avatar
YSelf Tool committed
111
                <h3 class="{{ title_text_class}}">{{ title if title is not none else action_text }}</h3>
YSelf Tool's avatar
YSelf Tool committed
112 113 114 115 116 117 118 119 120 121 122
            </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
123 124
                    {% elif f.type == "SelectMultipleField" %}
                        {{ render_selectmultiplefield(f) }}
125 126 127 128
                    {% elif f.type == "DateField" %}
                        {{ render_datefield(f) }}
                    {% elif f.type == "HiddenField" %}
                        {{ render_hiddenfield(f) }}
YSelf Tool's avatar
YSelf Tool committed
129 130 131 132 133 134 135
                    {% else %}
                        {{ f.type }}
                        {{ render_field(f) }}
                    {% endif %}
                {% endfor %}
            </div>
            <div class="{{ action_class }}">
136 137 138 139 140 141 142 143
                {% if btn_list != none %}
                    {%for this_btn_class, action_text, btn_value in btn_list %}
	                <button type="submit" class="{{ btn_class }} {{ this_btn_class }}", value="{{ btn_value }}">{{ action_text }}</button>
                    {% endfor %}
		{% else %}
                <button type="submit" class="{{ btn_class }}", value="{{ btn_value }}">{{ action_text }}</button>
                {% endif %}
                
YSelf Tool's avatar
YSelf Tool committed
144 145 146 147
            </div>
        </form>
    </div>
{%- endmacro %}
148