macros.html 7.67 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", textarea_rows=5) -%}
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
        {% if caller %}
            {{ caller() }}
        {% else %}
            {% for f in form %}
                {% if f.type == 'BooleanField' %}
                    {{ render_checkbox_field(f) }}
                {% elif f.type == 'RadioField' %}
                    {{ render_radio_field(f) }}
102
103
                {% elif f.type == 'TextAreaField' %}
                    {{ render_field(f, label_visible=labels_visible, rows=textarea_rows) }}
104
                {% else %}
Robin Sonnabend's avatar
Robin Sonnabend committed
105
                    {{ render_field(f, label_visible=labels_visible) }}
106
107
108
                {% endif %}
            {% endfor %}
        {% endif %}
109
        <button type="submit" class="{{btn_class}}">{{action_text}}</button>
110
111
    </form>
{%- endmacro %}
112
113

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

{% 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 %}
Robin Sonnabend's avatar
Robin Sonnabend committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

{% macro render_likes(likes) -%}
    {% set timestamp = now() %}
    {% if timestamp.month == 4 and timestamp.day == 1 %}
        {% set user=current_user() %}
        {% set add_link = user is not none and likes|selectattr("who", "equalto", user.username)|list|length == 0 %}
        {% set verb = "like" %}
        {% if likes|length == 1 %}
            {% set verb = "likes" %}
        {% endif %}
        {% if add_link %}
        <a href="{{url_for("new_like", next=request.url, **kwargs)}}">
        {% endif %}
        <div class="likes-div">
            <p>{{likes|length}} <span class="like-sign">&#x1f44d;</span></p>
                {% if user is not none or likes|length > 0 %}
                <p>
                    {% if likes|length == 0 %}
                        Be the first one to like this!
                    {% else %}
                        {{likes|map(attribute="who")|map("capitalize")|fancy_join(" and ")}} {{verb}} this 
                    {% endif %}
                </p>
            {% endif %}
        </div>
        {% if add_link %}
        </a>
        {% endif %}
    {% endif %}
{%- endmacro %}