From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 20 Jan 2025 12:33:22 +0000 (+0100)
Subject: Refactor templates.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bdb.prefix%7D%7D/static/condition?a=commitdiff_plain;h=834df9102bb71a8bcbda291d6504a6a45b357e78;p=plomledger

Refactor templates.
---

diff --git a/templates/_base.tmpl b/templates/_base.tmpl
index 8e6de41..2953be2 100644
--- a/templates/_base.tmpl
+++ b/templates/_base.tmpl
@@ -1,3 +1,4 @@
+{% import '_macros.tmpl' as macros %}
 <!DOCTYPE html>
 <html>
 <head>
diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl
new file mode 100644
index 0000000..4df4fc6
--- /dev/null
+++ b/templates/_macros.tmpl
@@ -0,0 +1,42 @@
+{% macro css_value_line() %}
+td.amt { text-align: right }
+td.amt, td.curr { font-family: monospace; font-size: 1.3em; }
+td.curr { text-align: center; }
+{% endmacro %}
+
+{% macro table_dat_lines(dat_lines, single, raw) %}
+<table>
+{% for dat_line in dat_lines %}
+  {% if (not (raw or single)) and dat_line.type == "intro" and loop.index > 1 %}
+    <tr><td colspan=5>&nbsp;</td></tr>
+  {% endif %}
+  <tr class="{{dat_line.type}}">
+  {% if not single %}
+    {% if dat_line.type == "intro" %}
+      <td id="{{dat_line.booking_id}}"><a href="#{{dat_line.booking_id}}">#</a></td>
+    {% else %}
+      <td></td>
+    {% endif %}
+  {% endif %}
+  {% if raw %}
+    {% if dat_line.type == "intro" %}
+      <td><a href="/booking/{{dat_line.booking_id}}"/>{{dat_line.raw_nbsp}}</a></td>
+    {% else %}
+      <td>{{dat_line.raw_nbsp}}</td>
+    {% endif %}
+  {% else %}
+    {% if dat_line.type == "intro" %}
+      <td class="code" colspan=3><a href="/booking/{{dat_line.booking_id}}">{{dat_line.code}}</a></td>
+    {% elif dat_line.type == "value" %}
+      <td class="amt">{{dat_line.booking_line.amt}}</td>
+      <td class="curr">{{dat_line.booking_line.curr|truncate(4,true,"…")}}</td>
+      <td>{{dat_line.booking_line.acc}}</td>
+    {% else %}
+      <td colspan=3>{{dat_line.code}}</td>
+    {% endif %}
+    <td>{{dat_line.comment}}</td>
+  {% endif %}
+  </tr>
+{% endfor %}
+</table>
+{% endmacro %}
diff --git a/templates/booking.tmpl b/templates/booking.tmpl
index 1bfea62..86273b9 100644
--- a/templates/booking.tmpl
+++ b/templates/booking.tmpl
@@ -1,25 +1,10 @@
 {% extends '_base.tmpl' %}
 
 {% block css %}
-td.amt { text-align: right }
-td.amt, td.curr { font-family: monospace; font-size: 1.3em; }
-td.curr { text-align: center; }
+{{ macros.css_value_line() }}
 {% endblock %}
 
 {% block content %}
-<table>
-{% for l in dat_lines %}
-  <tr class="{{l.type}}">
-  {% if l.type == "value" %}
-    <td class="amt">{{l.booking_line.amt}}</td><td class="curr">{{l.booking_line.curr|truncate(4,true,"…")}}</td><td>{{l.booking_line.acc}}</td>
-  {% elif l.type == "intro" %}
-    <td class="code" colspan=3><a href="/booking/{{l.booking_id}}">{{l.code}}</a></td>
-  {% else %}
-    <td colspan=3>{{l.code}}</td>
-  {% endif %}
-  <td>{{l.comment}}</td>
-  </tr>
-{% endfor %}
-</table>
+{{ macros.table_dat_lines(dat_lines, single=true, raw=false) }}
 {% endblock %}
 
diff --git a/templates/index.tmpl b/templates/index.tmpl
index 301b2d4..fc24e6e 100644
--- a/templates/index.tmpl
+++ b/templates/index.tmpl
@@ -1,33 +1,12 @@
 {% extends '_base.tmpl' %}
 
 {% block css %}
-td.amt { text-align: right }
-td.amt, td.curr { font-family: monospace; font-size: 1.3em; }
-td.curr { text-align: center; }
+{{ macros.css_value_line() }}
 {% endblock %}
 
 {% block content %}
 <form action="/" method="POST"><input type="submit" value="A" /></form>
 <form action="/" method="GET"><input type="submit" value="B" /></form>
 <hr/>
-<table>
-{% for l in dat_lines %}
-  {% if "intro" == l.type and loop.index > 1 %}<tr><td colspan=5>&nbsp;</td></tr>{% endif %}
-  <tr class="{{l.type}}">
-  {% if l.type == "intro" %}
-    <td id="{{l.booking_id}}"><a href="#{{l.booking_id}}">#</a></td>
-  {% else %}
-    <td></td>
-  {% endif %}
-  {% if l.type == "value" %}
-    <td class="amt">{{l.booking_line.amt}}</td><td class="curr">{{l.booking_line.curr|truncate(4,true,"…")}}</td><td>{{l.booking_line.acc}}</td>
-  {% elif l.type == "intro" %}
-    <td class="code" colspan=3><a href="/booking/{{l.booking_id}}">{{l.code}}</a></td>
-  {% else %}
-    <td colspan=3>{{l.code}}</td>
-  {% endif %}
-  <td>{{l.comment}}</td>
-  </tr>
-{% endfor %}
-</table>
+{{ macros.table_dat_lines(dat_lines, single=false, raw=false) }}
 {% endblock %}
diff --git a/templates/raw.tmpl b/templates/raw.tmpl
index 59e951f..6851d93 100644
--- a/templates/raw.tmpl
+++ b/templates/raw.tmpl
@@ -5,18 +5,6 @@ table { font-family: monospace; }
 {% endblock %}
 
 {% block content %}
-<table>
-{% for l in dat_lines %}
-  <tr class="{{l.type}}">
-  {% if l.type == "intro" %}
-    <td id="{{l.booking_id_}}"><a href="#{{l.booking_id}}">#</a></td>
-    <td><a href="/booking/{{l.booking_id}}"/>{{l.raw_nbsp}}</a></td>
-  {% else %}
-    <td></td>
-    <td>{{l.raw_nbsp}}</td>
-  {% endif %}
-  </tr>
-{% endfor %}
-</table>
+{{ macros.table_dat_lines(dat_lines, single=false, raw=true) }}
 {% endblock %}