From ed46fca603f3b587052f07a2417c34f52a5f866c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Wed, 26 Dec 2018 15:27:02 +0100
Subject: [PATCH] templates: add |as_template to render form with extended
 markup (#29355)

---
 gadjo/templates/gadjo/form.html   | 48 +++++++++++++------------------
 gadjo/templates/gadjo/widget.html | 33 +++++++++++++++++++++
 gadjo/templatetags/gadjo.py       |  6 ++++
 3 files changed, 59 insertions(+), 28 deletions(-)
 create mode 100644 gadjo/templates/gadjo/widget.html

diff --git a/gadjo/templates/gadjo/form.html b/gadjo/templates/gadjo/form.html
index 630ada8..34b89c6 100644
--- a/gadjo/templates/gadjo/form.html
+++ b/gadjo/templates/gadjo/form.html
@@ -1,34 +1,26 @@
 {% load i18n %}
-{{ form.non_field_errors }}
+{% if form.errors %}
+  <div class="errornotice">
+    <p>{% trans "There were errors processing your form." %}</p>
+    {% for error in form.non_field_errors %}
+    <p>{{ error }}</p>
+    {% endfor %}
+    {% for field in form %}
+      {% if field.is_hidden and field.errors %}
+        <p>
+          {% for error in field.errors %}
+          {% blocktrans with name=field.name %}(Hidden field {{name}}) {{ error }}{% endblocktrans %}
+          {% if not forloop.last %}<br>{% endif %}
+          {% endfor %}
+        </p>
+      {% endif %}
+    {% endfor %}
+  </div>
+{% endif %}
 {% for field in form %}
   {% if field.is_hidden %}
-    {% if field.errors %}
-      <ul class="errorlist">
-        {% for error in field.errors %}
-        <li>
-          {% blocktrans with name=field.name %}(Hidden field {{name}}) {{ error }}{% endblocktrans %}
-        </li>
-        {% endfor %}
-      </ul>
-    {% endif %}
     {{ field }}
-  {% endif %}
-{% endfor %}
-{% for field in form %}
-  {% if not field.is_hidden %}
-  <p {% if field.css_classes %}class="{{ field.css_classes }}"{% endif %}>
-     {{ field.label_tag }}
-     {% if field.errors %}
-       <ul class="errorlist">
-         {% for error in field.errors %}
-           <li>{{ error }}</li>
-         {% endfor %}
-       </ul>
-     {% endif %}
-     {{ field }}
-     {% if field.help_text %}
-       <span class="helptext">{{ field.help_text }}</span>
-     {% endif %}
-  </p>
+  {% else %}
+    {% include "gadjo/widget.html" with field=field %}
   {% endif %}
 {% endfor %}
diff --git a/gadjo/templates/gadjo/widget.html b/gadjo/templates/gadjo/widget.html
new file mode 100644
index 0000000..cde9f9d
--- /dev/null
+++ b/gadjo/templates/gadjo/widget.html
@@ -0,0 +1,33 @@
+<div class="widget
+            {{ field.css_classes }}
+            {% if field.errors %}widget-with-error{% endif %}
+            {% if field.required %}widget-required{% else %}widget-optional{% endif %}"
+     id="{{field.id_for_label}}_p">
+  {% block widget-title %}
+  <div class="title">
+    {{ field.label_tag }}
+    {% if field.field.required %}<span class="required">*</span>{% endif %}
+  </div>
+  {% endblock %}
+  {% block widget-content %}
+  <div class="content">
+    {% block widget-error %}
+    {% if field.errors %}
+    <div class="error">
+      {% for error in field.errors %}
+      {{ error }}{% if not forloop.last %}<br>{% endif %}
+      {% endfor %}
+    </div>
+    {% endif %}
+    {% endblock %}
+    {% block widget-control %}
+    {{ field }}
+    {% endblock %}
+    {% block widget-hint %}
+    {% if field.help_text %}
+    <div class="hint">{{ field.help_text|safe }}</div>
+    {% endif %}
+    {% endblock %}
+  </div>
+  {% endblock %}
+</div>
diff --git a/gadjo/templatetags/gadjo.py b/gadjo/templatetags/gadjo.py
index caed02f..08a34fa 100644
--- a/gadjo/templatetags/gadjo.py
+++ b/gadjo/templatetags/gadjo.py
@@ -116,3 +116,9 @@ def querystring(parser, token):
         raise TemplateSyntaxError("Malformed arguments to '%s'" % tag)
     removals = [parser.compile_filter(bit) for bit in bits]
     return QuerystringNode(updates, removals)
+
+
+@register.filter
+def as_template(form):
+    form_template = template.loader.get_template('gadjo/form.html')
+    return form_template.render({'form': form})
-- 
2.20.1

