|
@@ -19,12 +19,13 @@
|
|
|
#
|
|
|
|
|
|
|
|
|
# Django imports.
|
|
|
from django import template
|
|
|
from django.core import urlresolvers
|
|
|
from django.utils.html import format_html
|
|
|
|
|
|
|
|
|
# Get an instance of Django's template library.
|
|
|
register = template.Library()
|
|
|
|
|
|
|
|
@@ -51,32 +52,40 @@ def html_link(text, view, *args, **kwarg
|
|
|
title - Title for the HTML <a> element.
|
|
|
|
|
|
get - Additional GET parameter that should be appended to the URL.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# Verify the passed-in keyword arguments first.
|
|
|
for key in kwargs.keys():
|
|
|
if key not in ("get", "class", "title", "id"):
|
|
|
raise template.TemplateSyntaxError("Unknown argument for 'html_link' tag: %r" % key)
|
|
|
|
|
|
# Generate the URL by using the supplied view name and arguments that should
|
|
|
# be passed to the view.
|
|
|
url = urlresolvers.reverse(view, args=args)
|
|
|
|
|
|
# Set-up the base pattern (url, parameters, text).
|
|
|
pattern = '<a href="%s" %s>%s</a>'
|
|
|
if 'get' in kwargs:
|
|
|
pattern = '<a href="{url}?{get}"'
|
|
|
else:
|
|
|
pattern = '<a href="{url}"'
|
|
|
|
|
|
if 'class' in kwargs:
|
|
|
pattern += ' class="{class}"'
|
|
|
|
|
|
# Iterate over keyword arguments, and if they're supported, add them to
|
|
|
# parameters.
|
|
|
params = ""
|
|
|
for key, value in kwargs.iteritems():
|
|
|
if key in ("class", "title", "id"):
|
|
|
params += '%s="%s" ' % (key, value)
|
|
|
elif key == "get":
|
|
|
url += "?%s" % value
|
|
|
else:
|
|
|
raise template.TemplateSyntaxError("Unknown argument for 'advhtml_link' tag: %r" % key)
|
|
|
if 'title' in kwargs:
|
|
|
pattern += ' title="{title}"'
|
|
|
|
|
|
if 'id' in kwargs:
|
|
|
pattern += ' id="{id}"'
|
|
|
|
|
|
pattern += '>{text}</a>'
|
|
|
|
|
|
# Render the tag.
|
|
|
return pattern % (url, params, text)
|
|
|
return format_html(pattern, url=url, text=text, **kwargs)
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=True)
|
|
|
def active_link(context, url_name, return_value='active', **kwargs):
|
|
|
"""
|
|
|
This template tag can be used to check if the provided URL matches against
|