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

This commit is contained in:
Danielle McLean 2017-10-27 09:35:56 +11:00
parent 67308911fb
commit 29da1e52d7
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
4 changed files with 54 additions and 7 deletions

View file

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

View file

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

View file

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

View file

@ -71,8 +71,39 @@ def nav_right(request):
return {'items': items, 'request': request} return {'items': items, 'request': request}
@register.inclusion_tag('lemoncurry/tags/breadcrumbs.html') @register.inclusion_tag('lemoncurry/tags/breadcrumbs.html', takes_context=True)
def nav_crumbs(route, title): def nav_crumbs(context, route):
crumbs = breadcrumbs.find(route) crumbs = breadcrumbs.find(route)
current = crumbs.pop() 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'],
}