From 3425dbe3e96a1e9fc38d47fed6ea93c3bbe4bcda Mon Sep 17 00:00:00 2001
From: Thomas Schneider <thomas@fsmpi.rwth-aachen.de>
Date: Mon, 9 Sep 2024 18:13:47 +0200
Subject: [PATCH] instance, examples/templates: Better defaults handling

A Schild template should be able to be invoked without more context/variables
than Flask and Jinja2 already provide.

This also slightly moves the image path handling.
---
 examples/templates/_layout.html.j2    |  1 +
 examples/templates/image-left.html.j2 | 12 +++++++++---
 examples/templates/text-only.html.j2  |  2 +-
 schilder2000/instance.py              | 10 ++--------
 4 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/examples/templates/_layout.html.j2 b/examples/templates/_layout.html.j2
index ac9d804..9056e07 100644
--- a/examples/templates/_layout.html.j2
+++ b/examples/templates/_layout.html.j2
@@ -1,3 +1,4 @@
+{%- set schild = schild | default(None) -%}
 <!doctype html>
 <html lang="{% block lang %}de{% endblock %}">
 	<head>
diff --git a/examples/templates/image-left.html.j2 b/examples/templates/image-left.html.j2
index aa4c93c..7ef3763 100644
--- a/examples/templates/image-left.html.j2
+++ b/examples/templates/image-left.html.j2
@@ -5,10 +5,16 @@
 {% endblock title %}
 {%- block content -%}
 	<div class="container">
-		<img id="image" src="{{ url_for('instance.static',
-		filename=schild.image) if schild.image else 'placeholder' }}" />
+		<img
+			id="image"
+			{% if schild.image -%}
+				src="{{ url_for('instance.static', filename='img/'+schild.image) }}"
+			{%- else -%}
+				src="{{ url_for('instance.static', filename='img/sample.png') }}"
+			{%- endif -%}
+		/>
 		<p id="text">
-			{{ schild.text|default("Inhalt") }}
+			{{ schild.text|default(lipsum(n=1, min=5, max=10)) }}
 		</p>
 	</div>
 {% endblock content %}
diff --git a/examples/templates/text-only.html.j2 b/examples/templates/text-only.html.j2
index c65bf76..4f51c29 100644
--- a/examples/templates/text-only.html.j2
+++ b/examples/templates/text-only.html.j2
@@ -6,7 +6,7 @@
 {%- block content -%}
 	<div>
 		<p id="text">
-			{{ schild.text|default("Inhalt") }}
+			{{ schild.text|default(lipsum(n=2, min=5, max=10)) }}
 		</p>
 	</div>
 {% endblock content %}
diff --git a/schilder2000/instance.py b/schilder2000/instance.py
index 9f59e75..adef3a1 100644
--- a/schilder2000/instance.py
+++ b/schilder2000/instance.py
@@ -2,7 +2,6 @@ from . import db
 from .models import Schild
 from .helpers import Blueprint, get_template_attribute
 
-import textwrap
 
 from flask import url_for
 
@@ -31,13 +30,8 @@ def schild_pdf(ident):
 
 @bp.route("/sample/<template>.html")
 def sample_html(template):
-    lipsum = textwrap.dedent("""
-    	Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
-    	tempor incididunt ut labore et dolore magna aliqua.
-    """)
-    return bp.render_template(
-        template, schild=dict(title="Überschrift", text=lipsum, image="sample.png")
-    )
+    return bp.render_template(template)
+
 
 @bp.route("/sample/<template>.pdf")
 def sample_pdf(template):
-- 
GitLab