Login

Django Breadcrumbs Snippet

Author:
flashingpumpkin
Posted:
May 5, 2009
Language:
Python
Version:
1.0
Tags:
django templatetag breadcrumbs
Score:
2 (after 2 ratings)

A simple tag to render breadcrumbs.

Usage: {% load breadcrumbs %} {% breadcrumbs "['Home','Home','home']" "['Articles','Articles','articles']" "['object','object','object.get_absolute_url']" %}

Loads up the template in "modules/breadcrumbs.html" and renders it with a list of items.

You can provide the tag either with plain text stuff and named urls as the third argument ( any more arguments per bracket-block is parsed as args / kwargs for the reverse() call ) or the object directly, and the script tries after failing the reverse() to resolve the provided arguments.

Have pun.

 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import simplejson
from django import template
from django.core.urlresolvers import reverse, NoReverseMatch

register = template.Library()

@register.tag
def breadcrumbs(parser, token):
    """
    Breadcrumbing tag
    {% breadcrumbs "['Home','Home','home']" "['Articles','Articles','articles']" "['object','object','object.get_absolute_url']" %}
    """
    bits = token.split_contents()
    if len(bits) < 2:
        raise template.TemplateSyntaxError, '%s takes exactly at least one argument' % bits[0]
    return Breadcrumbs(bits[1:])

class Breadcrumbs(template.Node):
    def __init__(self, bits):
        self.bits = bits


    def render(self, context):
        items = []

        for i, bit in enumerate(self.bits):
            bit = bit.replace('"', '')
            bit = bit.replace("'", '"')
            crumb = simplejson.loads(bit)

            item = dict(
                anchor = template.Template(crumb[0]).render(context),
                title = template.Template(crumb[1]).render(context),
            )

            try:
                args = crumb[3]
            except IndexError:
                args = None

            try:
                kwargs = crumb[4]
            except IndexError:
                kwargs = None

            try:
                # Try reversing the third argument in a crumb, or ...
                item['url'] = reverse(crumb[2], args = args, kwargs = kwargs)
            except NoReverseMatch:
                # ... try to resolve the variable's n urls
                item['anchor'] = template.Variable(crumb[0]).resolve(context)
                item['title'] = template.Variable(crumb[1]).resolve(context)
                item['url'] = template.Variable(crumb[2]).resolve(context)

            items.append(item)

        context['breadcrumbs'] = items

        return template.loader.get_template(
            'modules/breadcrumbs.html'
        ).render(context)

More like this

  1. DaGood breadcrumbs by drozzy 6 years, 7 months ago
  2. breadcrumbs by guettli 6 years, 12 months ago
  3. URL based breadcrumbs by phlex 7 years, 5 months ago
  4. Manager for something like __inall by apollo13 7 years, 9 months ago
  5. Markdown and Syntax Highlighting in Django by blinks 8 years, 5 months ago

Comments

Please login first before commenting.