Skip to content

Instantly share code, notes, and snippets.

@sdiehl
Created November 25, 2010 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdiehl/714905 to your computer and use it in GitHub Desktop.
Save sdiehl/714905 to your computer and use it in GitHub Desktop.
Conditional CSS Display Tag
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