Created
March 17, 2010 23:37
-
-
Save markpasc/335867 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class EndTagSet(object): | |
def __init__(self, *tags): | |
self.tags = set(tags) | |
def __contains__(self, token_contents): | |
# Grab out the command so we can match on the tag name, not the | |
# whole tag contents. | |
try: | |
command = token_contents.split()[0] | |
except IndexError: | |
# The template parser will discover this is an empty tag | |
# momentarily, so ignore this and let it error. | |
return False | |
return command in self.tags | |
@register.tag | |
def case(parser, token): | |
try: | |
tag_name, arg = token.contents.split(None, 1) | |
except ValueError: | |
raise TemplateSyntaxError("%r tag requires an expression argument" | |
% token.contents.split(None, 1)[0]) | |
log.debug("Yay case %r tag! Let's eat some nodes", arg) | |
testexpr = parser.compile_filter(arg) | |
nodelist = parser.parse(EndTagSet('case', 'default', 'endswitch')) | |
if log.isEnabledFor(logging.DEBUG): | |
next_token = parser.next_token() | |
log.debug(" Ate up through %r", next_token.contents) | |
parser.prepend_token(next_token) | |
return CaseNode(arg, testexpr, nodelist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment