Browse Source

Bring back support for https://schema.org/BreadcrumbList - it's gross but what can you do

pull/1/head
Danielle McLean 5 years ago
parent
commit
29da1e52d7
Signed by: 00dani
GPG Key ID: 5A5D2D1AFF12EEC5
  1. 6
      lemoncurry/templates/lemoncurry/layout.html
  2. 3
      lemoncurry/templates/lemoncurry/tags/breadcrumbs.html
  3. 15
      lemoncurry/templatetags/jsonify.py
  4. 37
      lemoncurry/templatetags/lemoncurry_tags.py

6
lemoncurry/templates/lemoncurry/layout.html

@ -1,7 +1,7 @@
{% load compress favtags lemoncurry_tags meta static %}<!doctype html>
<html lang="en" class="{% block html_class %}{% endblock %}">
<head{% meta_namespaces %}>{% site_name as site_name %}{% request_uri request as uri %}
<base href="{% request_origin request %}" />
<head{% meta_namespaces %}>{% site_name as site_name %}{% request_uri request as uri %}{% request_origin request as origin %}
<base href="{{ origin }}" />
<title class="p-name">{% if title %}{{ title }} ~ {% endif %}{{ site_name }}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
@ -37,7 +37,7 @@
</div>
</nav>
{% if request.resolver_match.view_name %}
{% nav_crumbs request.resolver_match.view_name title %}
{% nav_crumbs request.resolver_match.view_name %}
{% endif %}
</header>

3
lemoncurry/templates/lemoncurry/tags/breadcrumbs.html

@ -1,4 +1,4 @@
{% if crumbs %}
{% load jsonify %}{% if crumbs %}
<nav class="breadcrumbs" aria-label="breadcrumb" role="navigation">
<ol class="breadcrumb">
{% for crumb in crumbs %}
@ -6,5 +6,6 @@
{% endfor %}
<li class="breadcrumb-item active" aria-current="page">{% firstof current.label title %}</li>
</ol>
<script type="application/ld+json">{{ breadcrumb_list | jsonify }}</script>
</nav>
{% endif %}

15
lemoncurry/templatetags/jsonify.py

@ -0,0 +1,15 @@
from django import template
from django.core.serializers import serialize
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models.query import QuerySet
from django.utils.safestring import mark_safe
import json
register = template.Library()
@register.filter
def jsonify(value):
if isinstance(value, QuerySet):
return mark_safe(serialize('json', value))
return mark_safe(json.dumps(value, cls=DjangoJSONEncoder))

37
lemoncurry/templatetags/lemoncurry_tags.py

@ -71,8 +71,39 @@ def nav_right(request):
return {'items': items, 'request': request}
@register.inclusion_tag('lemoncurry/tags/breadcrumbs.html')
def nav_crumbs(route, title):
@register.inclusion_tag('lemoncurry/tags/breadcrumbs.html', takes_context=True)
def nav_crumbs(context, route):
crumbs = breadcrumbs.find(route)
current = crumbs.pop()
return {'crumbs': crumbs, 'current': current, 'title': title}
item_list_element = [{
'@type': 'ListItem',
'position': i + 1,
'item': {
'@id': context['origin'] + reverse(crumb['route']),
'@type': 'WebPage',
'name': crumb['label']
}
} for i, crumb in enumerate(crumbs)]
item_list_element.append({
'@type': 'ListItem',
'position': len(item_list_element) + 1,
'item': {
'id': context['uri'],
'@type': 'WebPage',
'name': current['label'] or context.get('title'),
}
})
breadcrumb_list = {
'@context': 'http://schema.org',
'@type': 'BreadcrumbList',
'itemListElement': item_list_element
}
return {
'breadcrumb_list': breadcrumb_list,
'crumbs': crumbs,
'current': current,
'title': context['title'],
}

Loading…
Cancel
Save