1 |
|
# This is a copy of http://djangosnippets.org/snippets/1289/
|
2 |
|
#
|
3 |
|
# it provides two template tags to use in HTML templates: breadcrumb and
|
4 |
|
# breadcrumb_url.
|
5 |
|
#
|
6 |
|
# The first allows creating of simple url, with the text portion and url
|
7 |
|
# portion. Or only unlinked text (as the last item in breadcrumb trail for
|
8 |
|
# example). The second, can actually take the named url with arguments.
|
9 |
|
# Additionally it takes a title as the first argument.
|
10 |
|
#
|
11 |
|
# Initial Author: Andriy Drozdyuk
|
12 |
|
|
13 |
|
|
14 |
|
from django import template
|
15 |
|
from django.template import loader, Node, Variable
|
16 |
|
from django.utils.encoding import smart_str, smart_unicode
|
17 |
|
from django.template.defaulttags import url
|
18 |
|
from django.template import VariableDoesNotExist
|
19 |
|
|
20 |
|
register = template.Library()
|
21 |
|
|
22 |
|
@register.tag
|
23 |
|
def breadcrumb(parser, token):
|
24 |
|
"""
|
25 |
|
Renders the breadcrumb.
|
26 |
|
Examples:
|
27 |
|
{% breadcrumb "Title of breadcrumb" url_var %}
|
28 |
|
{% breadcrumb context_var url_var %}
|
29 |
|
{% breadcrumb "Just the title" %}
|
30 |
|
{% breadcrumb just_context_var %}
|
31 |
|
|
32 |
|
Parameters:
|
33 |
|
-First parameter is the title of the crumb,
|
34 |
|
-Second (optional) parameter is the url variable to link to, produced by url tag, i.e.:
|
35 |
|
{% url 'person_detail' object.id as person_url %}
|
36 |
|
then:
|
37 |
|
{% breadcrumb person.name person_url %}
|
38 |
|
|
39 |
|
@author Andriy Drozdyuk
|
40 |
|
"""
|
41 |
|
return BreadcrumbNode(token.split_contents()[1:])
|
42 |
|
|
43 |
|
|
44 |
|
@register.tag
|
45 |
|
def breadcrumb_url(parser, token):
|
46 |
|
"""
|
47 |
|
Same as breadcrumb
|
48 |
|
but instead of url context variable takes in all the
|
49 |
|
arguments URL tag takes.
|
50 |
|
{% breadcrumb "Title of breadcrumb" person_detail person.id %}
|
51 |
|
{% breadcrumb person.name person_detail person.id %}
|
52 |
|
"""
|
53 |
|
|
54 |
|
bits = token.split_contents()
|
55 |
|
if len(bits)==2:
|
56 |
|
return breadcrumb(parser, token)
|
57 |
|
|
58 |
|
# Extract our extra title parameter
|
59 |
|
title = bits.pop(1)
|
60 |
|
token.contents = ' '.join(bits)
|
61 |
|
|
62 |
|
url_node = url(parser, token)
|
63 |
|
|
64 |
|
return UrlBreadcrumbNode(title, url_node)
|
65 |
|
|
66 |
|
|
67 |
|
class BreadcrumbNode(Node):
|
68 |
|
def __init__(self, vars):
|
69 |
|
"""
|
70 |
|
First var is title, second var is url context variable
|
71 |
|
"""
|
72 |
|
self.vars = map(Variable,vars)
|
73 |
|
|
74 |
|
def render(self, context):
|
75 |
|
title = self.vars[0].var
|
76 |
|
|
77 |
|
if title.find("'")==-1 and title.find('"')==-1:
|
78 |
|
try:
|
79 |
|
val = self.vars[0]
|
80 |
|
title = val.resolve(context)
|
81 |
|
except:
|
82 |
|
title = ''
|
83 |
|
|
84 |
|
else:
|
85 |
|
title=title.strip("'").strip('"')
|
86 |
|
title=smart_unicode(title)
|
87 |
|
|
88 |
|
url = None
|
89 |
|
|
90 |
|
if len(self.vars)>1:
|
91 |
|
val = self.vars[1]
|
92 |
|
try:
|
93 |
|
url = val.resolve(context)
|
94 |
|
except VariableDoesNotExist:
|
95 |
|
url = None
|
96 |
|
|
97 |
|
return create_crumb(title, url)
|
98 |
|
|
99 |
|
|
100 |
|
class UrlBreadcrumbNode(Node):
|
101 |
|
def __init__(self, title, url_node):
|
102 |
|
self.title = Variable(title)
|
103 |
|
self.url_node = url_node
|
104 |
|
|
105 |
|
def render(self, context):
|
106 |
|
title = self.title.var
|
107 |
|
|
108 |
|
if title.find("'")==-1 and title.find('"')==-1:
|
109 |
|
try:
|
110 |
|
val = self.title
|
111 |
|
title = val.resolve(context)
|
112 |
|
except:
|
113 |
|
title = ''
|
114 |
|
else:
|
115 |
|
title=title.strip("'").strip('"')
|
116 |
|
title=smart_unicode(title)
|
117 |
|
|
118 |
|
url = self.url_node.render(context)
|
119 |
|
return create_crumb(title, url)
|
120 |
|
|
121 |
|
|
122 |
|
def create_crumb(title, url=None):
|
123 |
|
"""
|
124 |
|
Helper function
|
125 |
|
"""
|
126 |
|
crumb = """<span class="breadcrumbs-arrow">""" \
|
127 |
|
""" > """ \
|
128 |
|
"""</span>"""
|
129 |
|
if url:
|
130 |
|
crumb = "%s <a href='%s'>%s</a>" % (crumb, url, title)
|
131 |
|
else:
|
132 |
|
crumb = "%s %s" % (crumb, title)
|
133 |
|
|
134 |
|
return crumb
|
135 |
|
-
|