diff --git a/conntrackt/templatetags/conntrackt.py b/conntrackt/templatetags/conntrackt.py new file mode 100644 --- /dev/null +++ b/conntrackt/templatetags/conntrackt.py @@ -0,0 +1,44 @@ +from django import template +from django.core import urlresolvers + +register = template.Library() + +@register.inclusion_tag('conntrackt/link.html') +def html_link(view, title, identifier = None, html_class = None): + ''' + A small wrapper for showing HTML links. + + Arguments: + + view - View name for which the URL should be shown. + + title - Title which will be shown as the link. + + identifier - Identifier which is passed to the view for processing. + + html_class - Class which should be assigned to the link. + ''' + + return {'view': view, 'identifier': identifier, 'title': title, 'html_class': html_class} + +@register.simple_tag(takes_context = True) +def active_link(context, url_name, return_value='active', **kwargs): + matches = current_url_equals(context, url_name, **kwargs) + return return_value if matches else '' + + +def current_url_equals(context, url_name, **kwargs): + resolved = False + try: + resolved = urlresolvers.resolve(context.get('request').path) + except: + pass + matches = resolved and resolved.url_name == url_name + if matches and kwargs: + for key in kwargs: + kwarg = kwargs.get(key) + resolved_kwarg = resolved.kwargs.get(key) + if not resolved_kwarg or kwarg != resolved_kwarg: + return False + return matches +