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
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 = ['%s/' % (esc(home_link))]
for crumb in path.strip("/").split("/"):
if crumb:
output.append('%(crumb)s/' % {'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