Login

DaGood breadcrumbs

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

Provides two template tags to use in your HTML templates: breadcrumb and breadcrumb_url. The first allows creating of simple url, with the text portion and url portion. Or only unlinked text (as the last item in breadcrumb trail for example). The second, can actually take the named url with arguments! Additionally it takes a title as the first argument.

This is a templatetag file that should go into your /templatetags directory.

Just change the path of the image in the method create_crumb and you are good to go!

Don't forget to {% load breadcrumbs %} at the top of your html template!

http://drozdyuk.blogspot.com/2009/02/dagood-django-breadcrumbs.html

  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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from django import template
from django.template import loader, Node, Variable
from django.utils.encoding import smart_str, smart_unicode
from django.template.defaulttags import url
from django.template import VariableDoesNotExist

register = template.Library()

@register.tag
def breadcrumb(parser, token):
	"""
	Renders the breadcrumb.
	Examples:
		{% breadcrumb "Title of breadcrumb" url_var %}
		{% breadcrumb context_var  url_var %}
		{% breadcrumb "Just the title" %}
		{% breadcrumb just_context_var %}

	Parameters:
	-First parameter is the title of the crumb,
	-Second (optional) parameter is the url variable to link to, produced by url tag, i.e.:
		{% url person_detail object.id as person_url %}
		then:
		{% breadcrumb person.name person_url %}

	@author Andriy Drozdyuk
	"""
	return BreadcrumbNode(token.split_contents()[1:])


@register.tag
def breadcrumb_url(parser, token):
	"""
	Same as breadcrumb
	but instead of url context variable takes in all the
	arguments URL tag takes.
		{% breadcrumb "Title of breadcrumb" person_detail person.id %}
		{% breadcrumb person.name person_detail person.id %}
	"""

	bits = token.split_contents()
	if len(bits)==2:
		return breadcrumb(parser, token)

	# Extract our extra title parameter
	title = bits.pop(1)
	token.contents = ' '.join(bits)

	url_node = url(parser, token)

	return UrlBreadcrumbNode(title, url_node)


class BreadcrumbNode(Node):
	def __init__(self, vars):
		"""
		First var is title, second var is url context variable
		"""
		self.vars = map(Variable,vars)

	def render(self, context):
		title = self.vars[0].var

		if title.find("'")==-1 and title.find('"')==-1:
			try:
				val = self.vars[0]
				title = val.resolve(context)
			except:
				title = ''

		else:
			title=title.strip("'").strip('"')
			title=smart_unicode(title)

		url = None

		if len(self.vars)>1:
			val = self.vars[1]
			try:
				url = val.resolve(context)
			except VariableDoesNotExist:
				print 'URL does not exist', val
				url = None

		return create_crumb(title, url)


class UrlBreadcrumbNode(Node):
	def __init__(self, title, url_node):
		self.title = Variable(title)
		self.url_node = url_node

	def render(self, context):
		title = self.title.var

		if title.find("'")==-1 and title.find('"')==-1:
			try:
				val = self.title
				title = val.resolve(context)
			except:
				title = ''
		else:
			title=title.strip("'").strip('"')
			title=smart_unicode(title)

		url = self.url_node.render(context)
		return create_crumb(title, url)


def create_crumb(title, url=None):
	"""
	Helper function
	"""
	crumb = """<span class="breadcrumbs-arrow">""" \
			"""<img src="/media/images/arrow.gif" alt="Arrow">""" \
			"""</span>"""
	if url:
		crumb =	"%s<a href='%s'>%s</a>" % (crumb, url, title)
	else:
		crumb = "%s&nbsp;&nbsp;%s" % (crumb, title)

	return crumb

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.