From 223c1a1a03e570ffda09c11c09d3625ef7b52c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Dlouh=C3=BD?= Date: Thu, 19 Nov 2020 15:48:53 +0100 Subject: [PATCH] don't render templates if the template tag is blank --- scribbler/templatetags/scribbler_tags.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scribbler/templatetags/scribbler_tags.py b/scribbler/templatetags/scribbler_tags.py index 33e6363..4dc30a2 100644 --- a/scribbler/templatetags/scribbler_tags.py +++ b/scribbler/templatetags/scribbler_tags.py @@ -22,6 +22,8 @@ TOKEN_BLOCK = template_base.TOKEN_BLOCK TOKEN_COMMENT = template_base.TOKEN_COMMENT +wrapper_template = template.loader.get_template('scribbler/scribble-wrapper.html') +blank_scribble = wrapper_template.render({}) register = template.Library() @@ -71,10 +73,7 @@ def render(self, context): else: scribble_template = template.Template(self.raw) scribble_context = build_scribble_context(scribble) - content = scribble_template.render(scribble_context, request) - wrapper_template = template.loader.get_template('scribbler/scribble-wrapper.html') context['scribble'] = scribble - context['rendered_scribble'] = content user = context.get('user', None) show_controls = False can_edit = False @@ -92,6 +91,10 @@ def render(self, context): context['can_edit_scribble'] = can_edit context['can_delete_scribble'] = can_delete context['raw_content'] = self.raw + if not bool(scribble.content.strip()) and not (can_edit or can_add): + return blank_scribble # Don't bother to render blank scribble + content = scribble_template.render(scribble_context, request) + context['rendered_scribble'] = content # render() takes a dict, so we have to extract the context dict from the object context_data = context.dicts[-1] return wrapper_template.render(context_data, request)