From fb1237f11124a37dd540df605df26aca7ada2388 Mon Sep 17 00:00:00 2001 From: reverend Date: Fri, 2 Apr 2021 22:00:13 +0200 Subject: [PATCH] set variable and partial template tag --- .../templates/place/place_detail.html | 15 ++-- .../lostplaces/templatetags/lostplaces.py | 87 +++++++++++++++++++ 2 files changed, 97 insertions(+), 5 deletions(-) diff --git a/django_lostplaces/lostplaces/templates/place/place_detail.html b/django_lostplaces/lostplaces/templates/place/place_detail.html index aba45ac..20788c6 100644 --- a/django_lostplaces/lostplaces/templates/place/place_detail.html +++ b/django_lostplaces/lostplaces/templates/place/place_detail.html @@ -4,6 +4,7 @@ {% load thumbnail %} {% load svg_icon %} +{% load lostplaces %} {% block additional_head %} @@ -26,7 +27,9 @@

{{ place.name }}

{% if place.placeimages.first.filename.hero.url %}
- {% include 'partials/image.html' with source_url=place.placeimages.first.filename.hero.url %} + {% partial image %} + {% set source_url place.placeimages.first.filename.hero.url %} + {% endpartial %}
{% endif %} @@ -38,13 +41,15 @@
{% url 'place_tag_submit' place_id=place.id as tag_submit_url%} - {% include 'partials/tagging.html' with config=tagging_config %} + {% partial tagging %} + {% set config=tagging_config %} + {% endpartial %}

{% trans 'Map links' %}

- {% include 'partials/osm_map.html' with config=mapping_config%} + {% partial osm_map config=mapping_config %}
- {% translate 'Images' as t_images %} - {% include 'partials/placeImageGrid.html' with headline=t_images image_list=place.placeimages.all%} + {% translate 'Images' as headline %} + {% partial "placeImageGrid" image_list=place.placeimages.all %}
diff --git a/django_lostplaces/lostplaces/templatetags/lostplaces.py b/django_lostplaces/lostplaces/templatetags/lostplaces.py index c2a09d9..1fe3649 100644 --- a/django_lostplaces/lostplaces/templatetags/lostplaces.py +++ b/django_lostplaces/lostplaces/templatetags/lostplaces.py @@ -6,6 +6,15 @@ 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, '') + + return string + @register.filter(name='proper_paginate') def proper_paginate(paginator, current_page, neighbors=2): if paginator.num_pages > 2*neighbors: @@ -24,3 +33,81 @@ def proper_paginate(paginator, current_page, neighbors=2): page_list = [f for f in range(start_index, end_index+1)] return page_list[:(2*neighbors + 1)] return paginator.page_range + + +class VariableNode(template.Node): + def __init__(self, name, content=None): + if content==None and '=' in name: + self.content = name.split('=')[1] + self.name = name.split('=')[0] + else: + self.content = content + self.name = name + + def render(self, context): + if type(self.content) is template.NodeList: + self.content = self.content.render(context) + + self.content = remove_formatting(self.content) + + self.content = template.Variable(self.content).resolve(context) + + context[self.name] = self.content + return '' + +class PartialNode(template.Node): + def __init__(self, name, block): + self.name = remove_formatting(name) + self.block = block + + def render(self, context): + if type(self.block) is template.NodeList: + self.block = self.block.render(context) + + self.block = remove_formatting(self.block) + + context = context.flatten() + context['block'] = self.block + t = template.loader.get_template('partials/%s.html' % self.name) + return t.render(context) + +@register.tag(name='set') +def set_block(parser, token): + split = token.split_contents() + if len(split) >= 1: + variable_name = split[1] + else: + raise template.TemplateSyntaxError('%r expects a variable name' % split[0]) + + if len(split) == 2: + content=None + if '=' not in variable_name: + content = parser.parse(('end%s'%split[0],)) + parser.delete_first_token() + return VariableNode(variable_name, content) + else: + return VariableNode(variable_name, split[2]) + +@register.tag(name='partial') +def partial(parser, token): + split = token.split_contents() + if len(split) >= 1: + partial_name = split[1] + else: + 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() + return PartialNode(partial_name, nodeList) + else: + nodeList = template.NodeList() + for parameter in split[2:]: + if '=' in parameter: + nodeList.append( + VariableNode(name=parameter) + ) + else: + raise template.TemplateSyntaxError('%r expects kwargs, got args' % split[0]) + return PartialNode(partial_name, nodeList) + \ No newline at end of file