Ticket #9154: #9154 - templates_optimizations.diff

File #9154 - templates_optimizations.diff, 5.5 KB (added by German M. Bravo, 16 years ago)

New patch that hopefully fixes all {{ block.super }} issues

  • django/template/loader_tags.py

     
     1import copy
     2
    13from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
    2 from django.template import Library, Node, TextNode
    3 from django.template.loader import get_template, get_template_from_string, find_template_source
     4from django.template import Library, Node, NodeList, TextNode
     5from django.template.loader import get_template
    46from django.conf import settings
    57from django.utils.safestring import mark_safe
    68
     
    4345        self.nodelist = nodelist
    4446        self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
    4547        self.template_dirs = template_dirs
     48        if self.parent_name_expr is None:
     49            self.compiled_parent = copy.deepcopy(self.get_parent(context=None))
     50        else:
     51            self.compiled_parent = None
    4652
    4753    def __repr__(self):
    4854        if self.parent_name_expr:
     
    6167        if hasattr(parent, 'render'):
    6268            return parent # parent is a Template object
    6369        try:
    64             source, origin = find_template_source(parent, self.template_dirs)
     70            return get_template(parent, self.template_dirs)
    6571        except TemplateDoesNotExist:
    6672            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
    67         else:
    68             return get_template_from_string(source, origin, parent)
    6973
    7074    def render(self, context):
    71         compiled_parent = self.get_parent(context)
     75        if self.compiled_parent is not None:
     76            compiled_parent = self.compiled_parent
     77        else:
     78            compiled_parent = self.get_parent(context)
    7279        parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
     80        old_parent_nodelists = {}
     81        added_extended_blocknodes = []
    7382        for block_node in self.nodelist.get_nodes_by_type(BlockNode):
    7483            # Check for a BlockNode with this node's name, and replace it if found.
    7584            try:
     
    8695                        # If the first non-text node is an extends, handle it.
    8796                        if isinstance(node, ExtendsNode):
    8897                            node.nodelist.append(block_node)
     98                            added_extended_blocknodes.append(block_node.name)
    8999                        # Extends must be the first non-text node, so once you find
    90100                        # the first non-text node you can stop looking.
    91101                        break
     
    93103                # Keep any existing parents and add a new one. Used by BlockNode.
    94104                parent_block.parent = block_node.parent
    95105                parent_block.add_parent(parent_block.nodelist)
     106                old_parent_nodelists[block_node.name] = parent_block.nodelist
    96107                parent_block.nodelist = block_node.nodelist
    97         return compiled_parent.render(context)
     108        rendered_string = compiled_parent.render(context)
     109        # Restore every original parent nodelist to the state prior rendering it
     110        for node_name, nodelist in old_parent_nodelists.items():
     111            parent_blocks[node_name].nodelist = nodelist
     112        # Remove added BlockNodes to the parent's ExtendsNode
     113        for node in compiled_parent.nodelist:
     114            if not isinstance(node, TextNode):
     115                if isinstance(node, ExtendsNode):
     116                    node.nodelist = NodeList(filter(lambda n: not isinstance(n, BlockNode) or n.name not in added_extended_blocknodes, node.nodelist))
     117                break
     118        return rendered_string
    98119
    99120class ConstantIncludeNode(Node):
    100121    def __init__(self, template_path):
     
    156177    uses the literal value "base" as the name of the parent template to extend,
    157178    or ``{% extends variable %}`` uses the value of ``variable`` as either the
    158179    name of the parent template to extend (if it evaluates to a string) or as
    159     the parent tempate itelf (if it evaluates to a Template object).
     180    the parent template itself (if it evaluates to a Template object).
    160181    """
    161182    bits = token.split_contents()
    162183    if len(bits) != 2:
  • django/template/loader.py

     
    2727
    2828template_source_loaders = None
    2929
     30_template_cache = {}
     31
    3032class LoaderOrigin(Origin):
    3133    def __init__(self, display_name, loader, name, dirs):
    3234        super(LoaderOrigin, self).__init__(display_name)
     
    7375            pass
    7476    raise TemplateDoesNotExist, name
    7577
    76 def get_template(template_name):
     78def get_template(template_name, dirs=None):
    7779    """
    7880    Returns a compiled Template object for the given template name,
    7981    handling template inheritance recursively.
    8082    """
    81     source, origin = find_template_source(template_name)
    82     template = get_template_from_string(source, origin, template_name)
     83    if not settings.TEMPLATE_DEBUG and template_name in _template_cache:
     84        template = _template_cache[template_name]
     85    else:
     86        source, origin = find_template_source(template_name, dirs)
     87        template = get_template_from_string(source, origin, template_name)
     88        _template_cache[template_name] = template
    8389    return template
    8490
    8591def get_template_from_string(source, origin=None, name=None):
Back to Top