Ticket #9154: templates_optimizations_r9285.diff

File templates_optimizations_r9285.diff, 4.5 KB (added by Manuel Saelices, 16 years ago)

New patch that fixes {{ block.super }} issue (updated to r9285)

  • django/template/loader_tags.py

     
     1import copy
     2
    13from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
    24from django.template import Library, Node, TextNode
    3 from django.template.loader import get_template, get_template_from_string, find_template_source
     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 = dict()
    7381        for block_node in self.nodelist.get_nodes_by_type(BlockNode):
    7482            # Check for a BlockNode with this node's name, and replace it if found.
    7583            try:
     
    93101                # Keep any existing parents and add a new one. Used by BlockNode.
    94102                parent_block.parent = block_node.parent
    95103                parent_block.add_parent(parent_block.nodelist)
     104                old_parent_nodelists[block_node.name] = parent_block.nodelist
    96105                parent_block.nodelist = block_node.nodelist
    97         return compiled_parent.render(context)
     106        rendered_string = compiled_parent.render(context)
     107        # Restore every original parent nodelist
     108        for node_name, nodelist in old_parent_nodelists.items():
     109            parent_blocks[node_name].nodelist = nodelist
     110        return rendered_string
    98111
    99112class ConstantIncludeNode(Node):
    100113    def __init__(self, template_path):
     
    156169    uses the literal value "base" as the name of the parent template to extend,
    157170    or ``{% extends variable %}`` uses the value of ``variable`` as either the
    158171    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).
     172    the parent template itself (if it evaluates to a Template object).
    160173    """
    161174    bits = token.contents.split()
    162175    if len(bits) != 2:
  • django/template/loader.py

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