#14169: Improve TemplateSyntaxError for invalid block tags
--------------------------------------+-------------------------------------
          Reporter:  filipd           |         Owner:  nobody                  
            
            Status:  new              |     Milestone:                          
            
         Component:  Template system  |       Version:  SVN                     
            
        Resolution:                   |      Keywords:  TemplateSyntaxError, 
exception value
             Stage:  Unreviewed       |     Has_patch:  0                       
            
        Needs_docs:  0                |   Needs_tests:  0                       
            
Needs_better_patch:  0                |  
--------------------------------------+-------------------------------------
Changes (by mk):

  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0

Comment:

 This is hard to do, next to impossible even with the current code base.

 In django.template.Parser.invalid_block_tag, we can determine which tags
 are allowed using self.tags. However, this dict only contains f.e.
 'block', not 'endblock', only 'blocktrans', not 'endblocktrans'. We have
 no way of knowing whether 'endblock' or 'endblocktrans' are valid tags
 because the template tag author can freely choose tag names as he/she
 wants. It gets even worse with {% plural %} inside a blocktrans-block...

 Here's my stupid attempt at a patch, if anyone wants to continue:

 {{{
 diff --git a/django/template/__init__.py b/django/template/__init__.py
 index c316786..de460d0 100644
 --- a/django/template/__init__.py
 +++ b/django/template/__init__.py
 @@ -330,6 +330,8 @@ class Parser(object):

      def invalid_block_tag(self, token, command, parse_until=None):
          if parse_until:
 +            if command not in self.tags:
 +                raise self.error(token, "Invalid block tag: '%s', did you
 forget to load it?" % command)
              raise self.error(token, "Invalid block tag: '%s', expected
 %s" % (command, get_text_list(["'%s'" % p for p in parse_until])))
          raise self.error(token, "Invalid block tag: '%s'" % command)

 diff --git a/tests/regressiontests/templates/tests.py
 b/tests/regressiontests/templates/tests.py
 index 9e2d175..3ca8bf8 100644
 --- a/tests/regressiontests/templates/tests.py
 +++ b/tests/regressiontests/templates/tests.py
 @@ -318,6 +318,16 @@ class Templates(unittest.TestCase):
          except TemplateSyntaxError, e:
              self.assertEquals(e.args[0], "Invalid block tag: 'endblock',
 expected 'else' or 'endif'")

 +        try:
 +            t = Template("{% if 1 %}lala{% anything 'something' %}{%
 endif %}")
 +        except TemplateSyntaxError, e:
 +            self.assertEquals(e.args[0], "Invalid block tag: 'anything',
 did you forget to load it?")
 +
 +        try:
 +            t = Template("{% load i18n %}{% if 1 %}lala{% trans
 'something' %}{% bla %}{% endif %}")
 +        except TemplateSyntaxError, e:
 +            self.assertEquals(e.args[0], "Invalid block tag: 'bla',
 expected 'else' or 'endif'")
 +
      def test_templates(self):
          template_tests = self.get_template_tests()
          filter_tests = filters.get_filter_tests()
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14169#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to