set variable and partial template tag

This commit is contained in:
reverend 2021-04-02 22:00:13 +02:00
parent bae4e13137
commit fb1237f111
2 changed files with 97 additions and 5 deletions

View File

@ -4,6 +4,7 @@
{% load thumbnail %}
{% load svg_icon %}
{% load lostplaces %}
{% block additional_head %}
<link rel="stylesheet" href="{% static 'maps/ol.css' %}" type="text/css">
@ -26,7 +27,9 @@
<h1 class="LP-Headline">{{ place.name }}</h1>
{% if place.placeimages.first.filename.hero.url %}
<div class="LP-PlaceDetail__Image">
{% 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 %}
</div>
{% endif %}
</header>
@ -38,13 +41,15 @@
<section class="LP-Section">
{% 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 %}
</section>
<section class="LP-Section">
<h1 class="LP-Headline">{% trans 'Map links' %}</h1>
{% include 'partials/osm_map.html' with config=mapping_config%}
{% partial osm_map config=mapping_config %}
<div class="LP-LinkList">
<ul class="LP-LinkList__Container">
<li class="LP-LinkList__Item"><a target="_blank" href="https://www.google.com/maps?q={{place.latitude|safe}},{{place.longitude|safe}}" class="LP-Link"><span class="LP-Text">Google Maps</span></a></li>
@ -90,8 +95,8 @@
</section>
<section class="LP-Section">
{% 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 %}
</section>
</article>

View File

@ -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)