Filter for a list if breadcrumbs. All necessary code included but obviously only the breadcrumbs method that is needed with necessary changes for esc().
I use it as
{% load breadcrumbs %}
{{ request.path|breadcrumbs:"" }}
enclosed in an unordered html list with id="breadcrumbs"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from django import template
from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape
register = template.Library()
def check_autoescape(autoescape):
if autoescape:
return conditional_escape
return lambda x: x
@register.filter
def breadcrumbs(path, args, autoescape=None):
"""Usage: url|breadcrumbs:"home_link,li_class
Pass home_link empty if you just want to set li_class
Returns <li> elements representing breadcrumbs of your current location"""
esc = check_autoescape(autoescape)
home_link, sep, li_class = args.partition(',')
if li_class:
li_class = ' class="%s"' % (esc(li_class))
link_base = '/'
output = ['<li><a title="/" href="/">%s/</a></li>' % (esc(home_link))]
for crumb in path.strip("/").split("/"):
if crumb:
output.append('<li><a title="%(link_base)s%(crumb)s" href="%(link_base)s%(crumb)s/"%(li_class)s>%(crumb)s/</a></li>' % {'link_base': link_base, 'crumb': esc(crumb), 'li_class': li_class})
link_base += '%s/' % (esc(crumb))
return mark_safe(''.join(output))
breadcrumbs.needs_autoescape = True
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.