macros.html 6.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- Taken from https://gist.github.com/bearz/7394681 and modified
to not render a label for the CRSFTokenField -->

{# Renders field for bootstrap 3 standards.

    Params:
        field - WTForm field
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ macros.render_field(form.email, placeholder='Input email', type='email') }}
#}
{% macro render_field(field, label_visible=true) -%}

    <div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
        {% if field.type != 'HiddenField' and field.type !='CSRFTokenField' and label_visible %}
            <label for="{{ field.id }}" class="control-label">{{ field.label }}</label>
Robin Sonnabend's avatar
Robin Sonnabend committed
19
            {#<span onclick="el=document.getElementById('{{field.id}}-description');el.style.display=(el.style.display=='none'?'flex':'none')" class="field-description-questionmark">?</span>#}
20
        {% endif %}
Robin Sonnabend's avatar
Robin Sonnabend committed
21
        {{ field(title=field.description, placeholder=field.label.text, class_='form-control', **kwargs) }}
22
23
24
25
26
27
        {% if field.errors %}
            {% for e in field.errors %}
                <p class="help-block">{{ e }}</p>
            {% endfor %}
        {% endif %}
    </div>
Robin Sonnabend's avatar
Robin Sonnabend committed
28
    {#<div id="{{field.id}}-description" style="display:none" class="field-description">{{field.description}}</div>#}
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{%- endmacro %}

{# Renders checkbox fields since they are represented differently in bootstrap
    Params:
        field - WTForm field (there are no check, but you should put here only BooleanField.
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ macros.render_checkbox_field(form.remember_me) }}
 #}
{% macro render_checkbox_field(field) -%}
    <div class="checkbox {% if field.errors %}has-error{% endif %}">
        <label>
            {{ field(type='checkbox', **kwargs) }} {{ field.label }} 
        </label>
Robin Sonnabend's avatar
Robin Sonnabend committed
45
        <!--<span onclick="el=document.getElementById('{{field.id}}-description');el.style.display=(el.style.display=='none'?'flex':'none')" class="field-description-questionmark">?</span>-->
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
79
80
81
82
83
84
85
86
87
88
89
        {% if field.errors %}
            {% for e in field.errors %}
                <p class="help-block">{{ e }}</p>
            {% endfor %}
        {% endif %}
    </div>
    <div id="{{field.id}}-description" style="display:none" class="field-description">{{field.description}}</div>
{%- endmacro %}

{# Renders radio field
    Params:
        field - WTForm field (there are no check, but you should put here only BooleanField.
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ macros.render_radio_field(form.answers) }}
 #}
{% macro render_radio_field(field) -%}
    {% for value, label, _ in field.iter_choices() %}
        <div class="radio">
            <label>
                <input type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}">{{ label }}
            </label>
        </div>
    {% endfor %}
{%- endmacro %}

{# Renders WTForm in bootstrap way. There are two ways to call function:
     - as macros: it will render all field forms using cycle to iterate over them
     - as call: it will insert form fields as you specify:
     e.g. {% call macros.render_form(form, action_url=url_for('login_view'), action_text='Login',
                                        class_='login-form') %}
                {{ macros.render_field(form.email, placeholder='Input email', type='email') }}
                {{ macros.render_field(form.password, placeholder='Input password', type='password') }}
                {{ macros.render_checkbox_field(form.remember_me, type='checkbox') }}
            {% endcall %}

     Params:
        form - WTForm class
        action_url - url where to submit this form
        action_text - text of submit button
        class_ - sets a class for form
    #}
90
{% macro render_form(form, action_url='', action_text='Submit', class_='', btn_class='btn btn-default', enctype=None, labels_visible=True, method="POST") -%}
91

92
93
    <form method="{{method}}" action="{{ action_url }}" role="form" class="{{ class_ }}"{% if enctype is not none %}enctype="{{enctype}}"{% endif %}>
        {#{{ form.hidden_tag() if form.hidden_tag }}#}
94
95
96
97
98
99
100
101
102
        {% if caller %}
            {{ caller() }}
        {% else %}
            {% for f in form %}
                {% if f.type == 'BooleanField' %}
                    {{ render_checkbox_field(f) }}
                {% elif f.type == 'RadioField' %}
                    {{ render_radio_field(f) }}
                {% else %}
Robin Sonnabend's avatar
Robin Sonnabend committed
103
                    {{ render_field(f, label_visible=labels_visible) }}
104
105
106
                {% endif %}
            {% endfor %}
        {% endif %}
107
        <button type="submit" class="{{btn_class}}">{{action_text}}</button>
108
109
    </form>
{%- endmacro %}
110
111

{% macro render_table(table) -%}
Robin Sonnabend's avatar
Robin Sonnabend committed
112
113
114
115
116
117
    <h3>
        {{table.title}}
        {% if table.newlink is not none %}
            <a href="{{table.newlink}}">{{table.newtext}}</a>
        {% endif %}
    </h3>
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    <table class="table table-striped">
        <thead>
            <tr>
                {% for header in table.headers() %}
                    <th>{{header}}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for row in table.rows() %}
                <tr>
                    {% for entry in row %}
                        <td>{{entry}}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </tbody>
    </table>
{%- endmacro %}
Robin Sonnabend's avatar
Robin Sonnabend committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

{% macro render_single_table(table) -%}
    <h3>
        {{table.title}}
        {% if table.newlink is not none %}
            <a href="{{table.newlink}}">{{table.newtext}}</a>
        {% endif %}
    </h3>
    <table class="table table-striped">
        <tbody>
            {% for key, value in zip(table.headers(), table.row()) %}
                <tr>
                    <td>{{key}}</td>
                    <td>{{value}}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{%- endmacro %}