Created
November 25, 2010 04:36
-
-
Save sdiehl/714905 to your computer and use it in GitHub Desktop.
Conditional CSS Display Tag
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 import template | |
from django.template.defaultfilters import stringfilter | |
register = template.Library() | |
# Usage: | |
# template.render({'showme': False }) | |
# {% conditional_display showme %} -> style="display: none" | |
@register.tag(name="conditional_display") | |
def do_display(parser, token): | |
tag_name, cond_variable = token.split_contents() | |
return DisplayNode(cond_variable) | |
class DisplayNode(template.Node): | |
def __init__(self, cond_variable): | |
self.condition = template.Variable(cond_variable) | |
def render(self, context): | |
# Resolve the argument out to Truthiness value | |
if self.condition.resolve(context): | |
return '' | |
else: | |
return 'style="display: none"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment