From 3569adfd13e0b9ed6c0329fe6007e48364d372e8 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Tue, 16 May 2017 23:45:41 +0200 Subject: [PATCH] Add macro --- templates/helpers/_forms.html | 19 +++++++++++++++++++ templates/layouts/master.html | 1 + templates/web/edit_contact.html | 13 +------------ templates/web/new_contact.html | 13 +------------ 4 files changed, 22 insertions(+), 24 deletions(-) create mode 100644 templates/helpers/_forms.html diff --git a/templates/helpers/_forms.html b/templates/helpers/_forms.html new file mode 100644 index 0000000..0742a2b --- /dev/null +++ b/templates/helpers/_forms.html @@ -0,0 +1,19 @@ +{% macro generate_fields(form) -%} + {% for field in form %} + {% if field.type != 'CSRFTokenField' %} +
+ {{ field.label }} + {% if field.type in ('StringField', 'PasswordField') %} + {{ field(class='form-control') }} + {% else %} + {{ field() }} + {% endif %} + {% for error in field.errors %} + {{ error }} + {% endfor %} +
+ {% else %} + {{ field() }} + {% endif %} + {% endfor %} +{%- endmacro %} diff --git a/templates/layouts/master.html b/templates/layouts/master.html index b5abfe3..a4754bc 100644 --- a/templates/layouts/master.html +++ b/templates/layouts/master.html @@ -1,3 +1,4 @@ +{% from 'helpers/_forms.html' import generate_fields with context %} diff --git a/templates/web/edit_contact.html b/templates/web/edit_contact.html index dd177fb..af0bae3 100644 --- a/templates/web/edit_contact.html +++ b/templates/web/edit_contact.html @@ -3,18 +3,7 @@ {% block body %}

Edit contact

- {{ form.csrf_token }} - {% for field in form %} - {% if field.label.text != 'CSRF Token' %} -
- {{ field.label }} - {{ field(class='form-control') }} - {% for error in field.errors %} - {{ error }} - {% endfor %} -
- {% endif %} - {% endfor %} + {{ generate_fields(form) }}
{% endblock %} diff --git a/templates/web/new_contact.html b/templates/web/new_contact.html index 8ca32e4..2cef33d 100644 --- a/templates/web/new_contact.html +++ b/templates/web/new_contact.html @@ -3,18 +3,7 @@ {% block body %}

New contact

- {{ form.csrf_token }} - {% for field in form %} - {% if field.label.text != 'CSRF Token' %} -
- {{ field.label }} - {{ field(class='form-control', value='') }} - {% for error in field.errors %} - {{ error }} - {% endfor %} -
- {% endif %} - {% endfor %} + {{ generate_fields(form) }}
{% endblock %}