macros.html 6.98 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
                <div class="mdl-card__supporting-text">
                    {{ e }}
                </div>
            {% endfor %}
        {% endif %}
    </div>
{%- endmacro %}

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
{% macro render_integerfield(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 }}"{% endif %} />
        <label class="mdl-textfield__label" for="{{ field.id }}">{{ field.label.text }}</label>
        {% if field.errors %}
            {% for e in field.errors %}
                <div class="mdl-card__supporting-text">
                    {{ e }}
                </div>
            {% endfor %}
        {% endif %}
    </div>
{%- endmacro %}



YSelf Tool's avatar
YSelf Tool committed
41
{% macro render_passwordfield(field) -%}
YSelf Tool's avatar
YSelf Tool committed
42
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
YSelf Tool's avatar
YSelf Tool committed
43 44 45
        <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
46
            {% for e in field.errors %}
YSelf Tool's avatar
YSelf Tool committed
47 48 49 50 51 52 53 54 55 56 57 58 59
                <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
60
    {% if field.errors %}
YSelf Tool's avatar
YSelf Tool committed
61
        {% for e in field.errors %}
YSelf Tool's avatar
YSelf Tool committed
62 63 64 65 66
            <div class="mdl-card__supporting-text">
                {{ e }}
            </div>
        {% endfor %}
    {% endif %}
YSelf Tool's avatar
YSelf Tool committed
67 68 69 70 71 72
{%- endmacro %}

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

YSelf Tool's avatar
YSelf Tool committed
73
{% macro render_selectmultiplefield(field) -%}
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    <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
101 102
        {% if field.errors %}
            {% for e in field.errors %}
YSelf Tool's avatar
YSelf Tool committed
103 104 105
                <div class="mdl-card__supporting-text">
                    {{ e }}
                </div>
YSelf Tool's avatar
YSelf Tool committed
106 107
            {% endfor %}
        {% endif %}
108
    </div>
YSelf Tool's avatar
YSelf Tool committed
109 110
{%- endmacro %}

111 112 113 114 115 116 117 118 119 120
{% 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
121 122


123
{% 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
124 125 126
    <div class="{{ class_ }}">
        <form method="POST" action="{{ action_url }}">
            <div class="{{ title_class }}">
YSelf Tool's avatar
YSelf Tool committed
127
                <h3 class="{{ title_text_class}}">{{ title if title is not none else action_text }}</h3>
YSelf Tool's avatar
YSelf Tool committed
128 129 130 131 132 133 134
            </div>
            <div class="{{ content_class }}">
                {% for f in form %}
                    {% if f.type == "StringField" %}
                        {{ render_stringfield(f) }}
                    {% elif f.type == "PasswordField" %}
                        {{ render_passwordfield(f) }}
135 136
                    {% elif f.type == "IntegerField" %}
                        {{ render_integerfield(f) }}
YSelf Tool's avatar
YSelf Tool committed
137 138 139 140
                    {% elif f.type == "BooleanField" %}
                        {{ render_booleanfield(f) }}
                    {% elif f.type == "CSRFTokenField" %}
                        {{ render_csrftokenfield(f, kwargs) }}
YSelf Tool's avatar
YSelf Tool committed
141 142
                    {% elif f.type == "SelectMultipleField" %}
                        {{ render_selectmultiplefield(f) }}
143 144 145 146
                    {% elif f.type == "DateField" %}
                        {{ render_datefield(f) }}
                    {% elif f.type == "HiddenField" %}
                        {{ render_hiddenfield(f) }}
YSelf Tool's avatar
YSelf Tool committed
147 148 149 150 151 152 153
                    {% else %}
                        {{ f.type }}
                        {{ render_field(f) }}
                    {% endif %}
                {% endfor %}
            </div>
            <div class="{{ action_class }}">
154 155
                {% if btn_list != none %}
                    {%for this_btn_class, action_text, btn_value in btn_list %}
156
	                <button type="submit" class="{{ btn_class }} {{ this_btn_class }}" name="submit" value="{{ btn_value }}">{{ action_text }}</button>
157 158
                    {% endfor %}
		{% else %}
159
                <button type="submit" class="{{ btn_class }}" value="{{ btn_value }}">{{ action_text }}</button>
160 161
                {% endif %}
                
YSelf Tool's avatar
YSelf Tool committed
162 163 164 165
            </div>
        </form>
    </div>
{%- endmacro %}
166