I want to extend if to pyif tag. And I also want to implement elif tag support, just like:
{% pyif i == 1 %}
<p>i=1</p>
{% elif i == 3 %}
<p>i=3</p>
{% else %}
<p>other</p>
{% endif %}
So you can see elif has en expression. But I found current django template/__init__.py can not support enclose tag process just like elif, because it has extra info. And the code in template/__init__.py is:
elif token.token_type == TOKEN_BLOCK:
if token.contents in parse_until:
# put token back on token list so calling code knows why it terminated
self.prepend_token(token)
return nodelist
try:
command = token.contents.split()[0]
except IndexError:
self.empty_block_tag(token)
So you can see django use token.contents in parse_until to determin if it encounter an enclose tag, and doesn't support extra info after the enclose tag. So if I extend elif and add extra expression, it'll failed and raise:
Invalid block tag: 'elif'
So I made this patch so that django can just judge the tagname but not the whole token.contents.