Skip to content

Commit

Permalink
Allow whitespace prior to an "extends" tag. This allows a little more…
Browse files Browse the repository at this point in the history
… formatting flexibility. Refs #6274.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7082 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Feb 4, 2008
1 parent 6674718 commit 10015fa
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions django/template/loader_tags.py
@@ -1,5 +1,5 @@
from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
from django.template import Library, Node from django.template import Library, Node, TextNode
from django.template.loader import get_template, get_template_from_string, find_template_source from django.template.loader import get_template, get_template_from_string, find_template_source
from django.conf import settings from django.conf import settings
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -62,7 +62,14 @@ def get_parent(self, context):


def render(self, context): def render(self, context):
compiled_parent = self.get_parent(context) compiled_parent = self.get_parent(context)
parent_is_child = isinstance(compiled_parent.nodelist[0], ExtendsNode) if len(compiled_parent.nodelist) > 1:
n0, n1 = compiled_parent.nodelist[:2]
else:
n0, n1 = compiled_parent.nodelist[0], None
parent_is_child = (isinstance(n0, ExtendsNode) or
(isinstance(n0, TextNode) and isinstance(n1, ExtendsNode)))
if parent_is_child:
extend_node = int(not isinstance(n0, ExtendsNode))
parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)]) parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
for block_node in self.nodelist.get_nodes_by_type(BlockNode): for block_node in self.nodelist.get_nodes_by_type(BlockNode):
# Check for a BlockNode with this node's name, and replace it if found. # Check for a BlockNode with this node's name, and replace it if found.
Expand All @@ -74,7 +81,7 @@ def render(self, context):
# add this BlockNode to the parent's ExtendsNode nodelist, so # add this BlockNode to the parent's ExtendsNode nodelist, so
# it'll be checked when the parent node's render() is called. # it'll be checked when the parent node's render() is called.
if parent_is_child: if parent_is_child:
compiled_parent.nodelist[0].nodelist.append(block_node) compiled_parent.nodelist[extend_node].nodelist.append(block_node)
else: else:
# Keep any existing parents and add a new one. Used by BlockNode. # Keep any existing parents and add a new one. Used by BlockNode.
parent_block.parent = block_node.parent parent_block.parent = block_node.parent
Expand Down

0 comments on commit 10015fa

Please sign in to comment.