Reputation: 58522
I was wondering if there was a way to check if a tag has an endblock. I am basically trying to let the user do
{% mytag 'a' 'b' 'c' %}
or
{% mytag 'a' 'b' 'c' %}
<!-- other markup here -->
{% end mytag %}
I saw if that its not there it will raise an exception, but is there any way to programmatically setup my tag to safely handle both situations ?
Upvotes: 2
Views: 297
Reputation: 231
You can try parsing until the closing tag and catching the exception if it isn't found. You might want to stop if you run in to another mytag node also:
def do_mytag_stuff(parser, token):
# Process your token however you need
mytag_args = token.split_contents()
try:
nodelist = parser.parse(('endmytag', 'mytag'))
token = parser.next_token()
if token.contents == 'endmytag':
# Found an ending tag, make a node for its contents
parser.delete_first_token()
return MyTagNode(nodelist, mytag_args)
except TemplateSyntaxError:
# Neither tag was found
pass
# So either there's no closing tag, or we met another mytag before a closing tag.
# Do whatever you would for a single tag here
return SingleMyTagNode(mytag_args)
Not sure if that's 100% correct, but hopefully it will help.
Upvotes: 3