Last active
January 12, 2017 09:48
-
-
Save tibbiyelininja/69aec1259131b5619fb7 to your computer and use it in GitHub Desktop.
Django Absolute URL Node with Active Property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template import Library | |
from django.template.defaulttags import URLNode, url | |
register = Library() | |
class AbsoluteURL(str): | |
pass | |
class AbsoluteURLNode(URLNode): | |
def render(self, context): | |
asvar, self.asvar = self.asvar, None | |
path = super(AbsoluteURLNode, self).render(context) | |
request_obj = context['request'] | |
abs_url = AbsoluteURL(request_obj.build_absolute_uri(path)) | |
if not asvar: | |
return str(abs_url) | |
else: | |
if path == request_obj.path: | |
abs_url.active = 'active' | |
else: | |
abs_url.active = '' | |
context[asvar] = abs_url | |
return '' | |
@register.tag | |
def absurl(parser, token): | |
node = url(parser, token) | |
return AbsoluteURLNode( | |
view_name=node.view_name, | |
args=node.args, | |
kwargs=node.kwargs, | |
asvar=node.asvar | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@JellisHeRo as same as default
url
tag arguments. Sorry for late response, I am posting this with assuming that you have not found the answer yet. Just replace yoururl
tags in your template withabsurl