Opened 17 years ago

Closed 10 years ago

#7377 closed Bug (worksforme)

"extends" and "block" tags are not available when constructing template from scratch

Reported by: mgeorge@… Owned by: Stephen Burrows
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: elsdoerfer@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

with a fresh svn chekcout, made a new project, and run:
./manage.py shell

In [1]: from django.template import Template, Context 

In [2]: t = Template('''{% block title %}Hello{% end block %}''') 
---------------------------------------------------------------------------
TemplateSyntaxError                       Traceback (most recent call last)

/Users/mattgeorge/django-code/gapp/<ipython console> in <module>()

/Library/Python/2.5/site-packages/django/template/__init__.py in __init__(self, template_string, origin, name)
    164         if settings.TEMPLATE_DEBUG and origin is None:
    165             origin = StringOrigin(template_string)
--> 166         self.nodelist = compile_string(template_string, origin)
    167         self.name = name
    168 

/Library/Python/2.5/site-packages/django/template/__init__.py in compile_string(template_string, origin)
    185     lexer = lexer_class(template_string, origin)
    186     parser = parser_class(lexer.tokenize())
--> 187     return parser.parse()
    188 
    189 class Token(object):

/Library/Python/2.5/site-packages/django/template/__init__.py in parse(self, parse_until)
    267                     compile_func = self.tags[command]
    268                 except KeyError:
--> 269                     self.invalid_block_tag(token, command)
    270                 try:
    271                     compiled_result = compile_func(self, token)

/Library/Python/2.5/site-packages/django/template/__init__.py in invalid_block_tag(self, token, command)
    319 
    320     def invalid_block_tag(self, token, command):
--> 321         raise self.error(token, "Invalid block tag: '%s'" % command)
    322 
    323     def unclosed_block_tag(self, parse_until):

TemplateSyntaxError: Invalid block tag: 'block'

however, this works:

Matt-George:gapp mattgeorge$./manage.py shell
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) 
Type "copyright", "credits" or "license" for more information.

IPython 0.8.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from django.template import Template, Context 

In [2]: from django.template.loader import *

In [3]: t = Template('''{% block title %}Hello{% endblock %}''')

it looks like the issue comes from the line:
add_to_builtins('django.template.loader_tags')

in django.template.loader

Attachments (1)

template_loader.diff (1.1 KB ) - added by Matt G <mgeorge@…> 17 years ago.

Download all attachments as: .zip

Change History (19)

comment:1 by Alex Gaynor, 17 years ago

Resolution: invalid
Status: newclosed

That error occurred because you had {% end block %} in place of {% endblock %}.

comment:2 by mgeorge@…, 17 years ago

Resolution: invalid
Status: closedreopened

that was a typo from an old terminal. It happens even with the {% endblock %}

In [1]: from django.template import Template, Context 

In [2]: t = Template('''{% block title %}Hello{% endblock %}''')
---------------------------------------------------------------------------
TemplateSyntaxError                       Traceback (most recent call last)

comment:3 by Alex Gaynor, 17 years ago

This is not a problem with Django:

In [1]: from django.template import Template

In [2]: t= Template('{% block title %}Hello{% endblock %}')

If you continue to have this problem try asking on the mailing-list or in #django on freenode, trac is not the correct place for handling usage problems.

comment:4 by Alex Gaynor, 17 years ago

Resolution: invalid
Status: reopenedclosed

comment:5 by Collin Grady, 17 years ago

Resolution: invalid
Status: closedreopened
Triage Stage: UnreviewedAccepted

by Matt G <mgeorge@…>, 17 years ago

Attachment: template_loader.diff added

comment:6 by Matt G <mgeorge@…>, 17 years ago

Has patch: set

comment:7 by Adrian Holovaty, 17 years ago

Triage Stage: AcceptedDesign decision needed

Hmm, I could see an argument for this *not* being a bug. If you're constructing a template from scratch, bypassing the template-loading infrastructure, using "{% extends %}" and "{% block %}" is meaningless. I'll leave it open for discussion, though.

comment:8 by Adrian Holovaty, 16 years ago

Summary: TemplateSyntaxError: Invalid block tag: 'block'"extends" and "block" tags are not available when constructing template from scratch

comment:9 by Johannes Dollinger, 16 years ago

This would be fixed by the refactoring proposed in #7806 (as a side effect).

comment:10 by Marty Alchin, 16 years ago

For what it's worth, {% extends %} and {% block %} aren't entirely meaningless outside the context of standard template-loading. Since {% extends %} can take a fully-formed Template object as its argument, it's quite possible (or at least, could be) to construct an inherited structure while still bypassing template loaders. Admittedly, using a string in {% extends %} without a template loader isn't realistic (and using {% block %} without {% extends %} would be useless), but I just wanted to point out one supported situation that is indeed impossible with this setup.

comment:11 by miracle2k, 16 years ago

Cc: elsdoerfer@… added

comment:12 by Luke Plant, 14 years ago

Severity: Normal
Type: Bug

comment:13 by Carl Meyer, 13 years ago

Easy pickings: unset
Triage Stage: Design decision neededAccepted
UI/UX: unset

Given Gulopine's comment, I don't see any reason not to treat this as a bug.

comment:14 by Andrew Badr, 12 years ago

FWIW all you have to do to get the desired behavior is import django.template.loader anywhere in your program. Magic!

comment:15 by Stephen Burrows, 12 years ago

Owner: changed from nobody to Stephen Burrows
Status: reopenednew

I've made a pull request which fixes this ticket: https://github.com/django/django/pull/414. This seems to just have been a matter of adding the loader tags to builtins at the same time that the defaulttags and defaultfilters are added. Doing so doesn't cause any new issues to appear... so unless there's something I'm missing, this should be good to go.

(I don't have tests, but I can't think of a way to add an automated test for this.)

comment:16 by Stephen Burrows, 12 years ago

Status: newassigned

comment:17 by Łukasz Rekucki, 12 years ago

Needs tests: set

@melinath: To test this, you can create a python subprocess that executes some code snippet in which you only import django.template, but not the loaders. It's also worth checking what happens in that scenario if I pass a string to extends.

comment:18 by ANUBHAV JOSHI, 10 years ago

Resolution: worksforme
Status: assignedclosed

I don't think that this exists anymore in master.

Note: See TracTickets for help on using tickets.
Back to Top