lenticular_cloud2/templates/base.html.j2

164 lines
6.1 KiB
Plaintext
Raw Normal View History

2020-05-09 18:00:07 +00:00
{% extends 'skelet.html.j2' %}
{# 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, horizontal=false) -%}
<div class="form-group {% if field.errors %}has-error{% endif %} {{ 'row' if horizontal }} {{ kwargs.pop('class_', '') }}">
{% if (field.type != 'HiddenField' and field.type !='CSRFTokenField') and label_visible %}
<label for="{{ field.id }}" class="control-label {{ 'col-sm-2 col-form-label' if horizontal }}">{{ field.label.text }}</label>
{% endif %}
<div class="{{ 'col-sm-10' if horizontal }}">
{{ field(class_='form-control', **kwargs) }}
</div>
{% if field.errors %}
{% for e in field.errors %}
<p class="alert alert-danger">{{ e }}</p>
{% endfor %}
{% endif %}
</div>
{%- 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">
<label>
{{ field(type='checkbox', **kwargs) }} {{ field.label }}
</label>
</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 submit 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_submit_field(form.answers) }}
#}
{% macro render_submit_field(field, btn_class='btn btn-primary') -%}
<input type="submit" class="{{ btn_class }}" value="{{ field.label.text }}" />
{%- endmacro %}
{# Renders form 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_submit_field(form.answers) }}
#}
{% macro render_form_field(field) -%}
<fieldset>
{#<legend>{{field.label}}</legend>#}
{{ _render_form(field, horizontal=true) }}
</fieldset>
{%- endmacro %}
{% macro render_list_field(field) -%}
<fieldset>
<legend>{{ field.label.text }}</legend>
<template id='{{field.name}}-template'>
<li class="list-group-item">
{{ _render_form(field.get_template(), horizontal=true) }}
<a class="btn btn-danger" onclick="fieldlist.remove(this)">Remove</a>
</li>
</template>
<ul class="list-group">
{% for _field in field %}
<li class="list-group-item">
{{ _render_field(_field) }}
<a class="btn btn-danger" onclick="fieldlist.remove(this)">Remove</a>
</li>
{% endfor %}
</ul>
<a class="btn btn-success" onclick="fieldlist.add('{{ field.name }}')">Add</a>
</fieldset>
{%- endmacro %}
{% macro _render_field(f) %}
{% if f.type == 'BooleanField' %}
{{ render_checkbox_field(f, **kwargs) }}
{% elif f.type == 'RadioField' %}
{{ render_radio_field(f, **kwargs) }}
{% elif f.type == 'SubmitField' %}
{{ render_submit_field(f, **kwargs) }}
{% elif f.type == 'FormField' %}
{{ render_form_field(f, **kwargs) }}
{% elif f.type == 'ModelFieldList' %}
{{ render_list_field(f, **kwargs) }}
{% elif f.type == 'FieldList' %}
{{ render_list_field(f, **kwargs) }}
{% else %}
{{ render_field(f, **kwargs) }}
{% endif %}
{% endmacro %}
{% macro _render_form(form) -%}
{% if caller %}
{{ caller() }}
{% else %}
{% for f in form %}
{{ _render_field(f, **kwargs) }}
{% endfor %}
{% endif %}
{%- 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
#}
{% macro render_form(form, action_url='', class_='', method='post') -%}
<form method="{{ method }}" {% if action_url %}action="{{ action_url }}" {% endif %}role="form" class="{{ class_ }}">
<input name="form" type="hidden" value="{{ form.__class__.__name__ }}">
{{ _render_form(form) }}
</form>
{%- endmacro %}