Commits

Mathiasdm committed 95c8a96

Allow rendering breadcrumbs using the template 'breadcrumbs/breadcrumb.html'

Comments (0)

Files changed (3)

breadcrumbs/templatetags/breadcrumbs_tags.py

 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
+from django.template import VariableDoesNotExist, loader, Context
 
 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 %}
+    """
+    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 %}
+    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:])
+    @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 %}
-	"""
+    """
+    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)
+    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)
+    # Extract our extra title parameter
+    title = bits.pop(1)
+    token.contents = ' '.join(bits)
 
-	url_node = url(parser, token)
+    url_node = url(parser, token)
 
-	return UrlBreadcrumbNode(title, url_node)
+    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 __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
+    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 = ''
+        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)
+        else:
+            title=title.strip("'").strip('"')
+            title=smart_unicode(title)
 
-		url = None
+        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
+        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)
+        return create_crumb(title, url)
 
 
 class UrlBreadcrumbNode(Node):
-	def __init__(self, title, url_node):
-		self.title = Variable(title)
-		self.url_node = url_node
+    def __init__(self, title, url_node):
+        self.title = Variable(title)
+        self.url_node = url_node
 
-	def render(self, context):
-		title = self.title.var
+    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)
+        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)
+        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)
+    """
+    Helper function
+    """
+    try:
+        t = loader.get_template("breadcrumbs/breadcrumb.html")
+        use_template = True
+    except:
+        use_template = False
 
-	return crumb
+    if use_template:
+        c = Context({'title': title, 'url': url})
+        return t.render(c)
+
+    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

breadcrumbs/tests.py

 import unittest
+import django
 from django.template import Context, Template
+from mock import patch
 
 def render(text, extra_context={}):
     c = Context()
             '{% breadcrumb "foo" bar %}'
         , extra_context={"bar": "/some/url"})
         self.assertEqual(result, '<span class="breadcrumbs-arrow"><img src="/media/images/arrow.gif" alt="Arrow"></span><a href=\'/some/url\'>foo</a>')
+
+    def test_render_breadcrumb_with_template(self):
+
+        class MockTemplate(object):
+            def render(self, c):
+                return "foo"
+
+        with patch.object(django.template.loader, 'get_template') as mock_get_template:
+            mock_get_template.return_value = MockTemplate()
+            result = render(
+                '{% load breadcrumbs_tags %}'
+                '{% breadcrumb "foo" bar %}'
+            , extra_context={"bar": "http://nowhere"})
+            self.assertEqual(result, "foo")
 Django>=1.4.0
+mock>=1.0.1