Login

Breadcrumbs filter

Author:
Oggu
Posted:
January 20, 2009
Language:
Python
Version:
1.0
Tags:
filter breadcrumbs
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. Breadcrumbs for flatpages by jca 7 years, 8 months ago
  2. URL based breadcrumbs by phlex 7 years, 5 months ago
  3. sublist by nofactor 7 years, 9 months ago
  4. split filter by Ciantic 8 years, 4 months ago
  5. django app name & breadcrumbs l10n by xormag 4 years, 1 month ago

Comments

Please login first before commenting.