From a139594863570be7330f7fd1645db5c4e754fc81 Mon Sep 17 00:00:00 2001 From: reverend Date: Tue, 6 Apr 2021 19:15:22 +0200 Subject: [PATCH] Fixin Partial and Set Tag --- .../lostplaces/templatetags/lostplaces.py | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/django_lostplaces/lostplaces/templatetags/lostplaces.py b/django_lostplaces/lostplaces/templatetags/lostplaces.py index 19fad77..b9d9520 100644 --- a/django_lostplaces/lostplaces/templatetags/lostplaces.py +++ b/django_lostplaces/lostplaces/templatetags/lostplaces.py @@ -7,11 +7,12 @@ from django.http import request register = template.Library() def remove_formatting(string): - for to_strip in ["'", '"', ' ']: - string = string.strip(to_strip) - - for to_remove in ['\t', '\n',]: - string = string.replace(to_remove, '') + if type(string) is str: + for to_strip in ["'", '"', ' ']: + string = string.strip(to_strip) + + for to_remove in ['\t', '\n',]: + string = string.replace(to_remove, '') return string @@ -50,11 +51,12 @@ class VariableNode(template.Node): def render(self, context): if type(self.content) is not str: self.content = self.content.render(context) + try: + self.content = template.Variable(self.content).resolve(context) + except template.VariableDoesNotExist: + pass self.content = remove_formatting(self.content) - - self.content = template.Variable(self.content).resolve(context) - context[self.name] = self.content return '' @@ -100,8 +102,20 @@ def partial(parser, token): raise template.TemplateSyntaxError('%r expects a partial name' % split[0]) if len(split) == 2: - nodeList = parser.parse(('end%s'%split[0],)) - parser.delete_first_token() + block_tag = False + for token in reversed(parser.tokens): + if 'end%s'%split[0] in token.contents: + block_tag = True + break + + if split[0] in token.contents: + break + + if block_tag: + nodeList = parser.parse(('end%s'%split[0],)) + parser.delete_first_token() + else: + nodeList = template.NodeList() return PartialNode(partial_name, nodeList) else: nodeList = template.NodeList()