Akron | ca9bd98 | 2016-12-06 16:59:57 +0100 | [diff] [blame] | 1 | {# Renders field for bootstrap 3 standards. |
| 2 | |
| 3 | Params: |
| 4 | field - WTForm field |
| 5 | kwargs - pass any arguments you want in order to put them into the html attributes. |
| 6 | There are few exceptions: for - for_, class - class_, class__ - class_ |
| 7 | |
| 8 | Example usage: |
| 9 | {{ macros.render_field(form.email, placeholder='Input email', type='email') }} |
| 10 | #} |
| 11 | {% macro render_field(field, label_visible=true) -%} |
| 12 | |
| 13 | <div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}"> |
| 14 | {% if (field.type != 'HiddenField' or field.type !='CSRFTokenField') and label_visible %} |
| 15 | <label for="{{ field.id }}" class="control-label">{{ field.label }}</label> |
| 16 | {% endif %} |
| 17 | {{ field(class_='form-control text-capitalized', **kwargs) }} |
| 18 | {% if field.errors %} |
| 19 | {% for e in field.errors %} |
| 20 | <p class="help-block">{{ e }}</p> |
| 21 | {% endfor %} |
| 22 | {% endif %} |
| 23 | </div> |
| 24 | {%- endmacro %} |
| 25 | |
| 26 | {# Renders checkbox fields since they are represented differently in bootstrap |
| 27 | Params: |
| 28 | field - WTForm field (there are no check, but you should put here only BooleanField. |
| 29 | kwargs - pass any arguments you want in order to put them into the html attributes. |
| 30 | There are few exceptions: for - for_, class - class_, class__ - class_ |
| 31 | |
| 32 | Example usage: |
| 33 | {{ macros.render_checkbox_field(form.remember_me) }} |
| 34 | #} |
| 35 | {% macro render_checkbox_field(field) -%} |
| 36 | <div class="checkbox"> |
| 37 | <label> |
| 38 | {{ field(type='checkbox', **kwargs) }} {{ field.label }} |
| 39 | </label> |
| 40 | </div> |
| 41 | {%- endmacro %} |
| 42 | |
| 43 | {# Renders radio field |
| 44 | Params: |
| 45 | field - WTForm field (there are no check, but you should put here only BooleanField. |
| 46 | kwargs - pass any arguments you want in order to put them into the html attributes. |
| 47 | There are few exceptions: for - for_, class - class_, class__ - class_ |
| 48 | |
| 49 | Example usage: |
| 50 | {{ macros.render_radio_field(form.answers) }} |
| 51 | #} |
| 52 | {% macro render_radio_field(field) -%} |
| 53 | {% for value, label, _ in field.iter_choices() %} |
| 54 | <div class="radio" style="margin:8pt auto;"> |
| 55 | <label> |
| 56 | <input type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}">{{ label }} |
| 57 | </label> |
| 58 | </div> |
| 59 | {% endfor %} |
| 60 | {%- endmacro %} |
| 61 | |
| 62 | {# Renders WTForm in bootstrap way. There are two ways to call function: |
| 63 | - as macros: it will render all field forms using cycle to iterate over them |
| 64 | - as call: it will insert form fields as you specify: |
| 65 | e.g. {% call macros.render_form(form, action_url=url_for('login_view'), action_text='Login', |
| 66 | class_='login-form') %} |
| 67 | {{ macros.render_field(form.email, placeholder='Input email', type='email') }} |
| 68 | {{ macros.render_field(form.password, placeholder='Input password', type='password') }} |
| 69 | {{ macros.render_checkbox_field(form.remember_me, type='checkbox') }} |
| 70 | {% endcall %} |
| 71 | |
| 72 | Params: |
| 73 | form - WTForm class |
| 74 | method - form method |
| 75 | action_url - url where to submit this form |
| 76 | action_text - text of submit button |
| 77 | class_ - sets a class for form |
| 78 | #} |
| 79 | {% macro render_form(form, action_url='', method='POST', action_text='Submit', class_='', btn_class='btn btn-default') -%} |
| 80 | |
| 81 | <form method="{{ method }}" action="{{ action_url }}" role="form" class="{{ class_ }}"> |
| 82 | {{ form.hidden_tag() if form.hidden_tag }} |
| 83 | {% if caller %} |
| 84 | {{ caller() }} |
| 85 | {% else %} |
| 86 | {% for f in form %} |
| 87 | {% if f.id != 'csrf_token' %} |
| 88 | {% if f.type == 'BooleanField' %} |
| 89 | {{ render_checkbox_field(f) }} |
| 90 | {% elif f.type == 'RadioField' %} |
| 91 | {{ render_radio_field(f) }} |
| 92 | {% else %} |
| 93 | {{ render_field(f) }} |
| 94 | {% endif %} |
| 95 | {% endif %} |
| 96 | {% endfor %} |
| 97 | {% endif %} |
| 98 | <button type="submit" class="{{ btn_class }}">{{ action_text }}</button> |
| 99 | </form> |
| 100 | {%- endmacro %} |