Login

Breadcrumbs filter

Author:
Oggu
Posted:
January 20, 2009
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.