Ticket #15053: patch.15426.diff

File patch.15426.diff, 5.9 KB (added by Pablo Martín, 14 years ago)
  • template/base.py

     
    195195                token = Token(TOKEN_COMMENT, content)
    196196        else:
    197197            token = Token(TOKEN_TEXT, token_string)
     198        token.source = self.origin, (-1, -1)
    198199        token.lineno = self.lineno
    199200        self.lineno += token_string.count('\n')
    200201        return token
     
    268269                raise TemplateSyntaxError("%r must be the first tag in the template." % node)
    269270        if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
    270271            nodelist.contains_nontext = True
     272        node.source = token.source
    271273        nodelist.append(node)
    272274
    273275    def enter_command(self, command, token):
  • template/loader_tags.py

     
    22from django.template.base import Library, Node, TextNode
    33from django.template.context import Context
    44from django.template.defaulttags import token_kwargs
    5 from django.template.loader import get_template
     5from django.template.loader import find_template, get_template
    66from django.conf import settings
    77from django.utils.safestring import mark_safe
    88
     
    9999            raise TemplateSyntaxError(error_msg)
    100100        if hasattr(parent, 'render'):
    101101            return parent # parent is a Template object
    102         return get_template(parent)
     102        origin, source = self.source
     103        return get_template(parent, skip_template=origin.name)
    103104
    104105    def render(self, context):
     106        origin, source = self.source
     107        if origin and origin.loadname == self.parent_name:
     108            template = find_template(self.parent_name, skip_template=origin.name)
     109            template_source = template[0]
     110            extends_tags = template_source.nodelist[0]
     111            parent_origin, parent_source = extends_tags.source
     112            self.parent_name = getattr(parent_origin, 'name', None)
    105113        compiled_parent = self.get_parent(context)
    106114
    107115        if BLOCK_CONTEXT_KEY not in context.render_context:
  • template/debug.py

     
    5454    def create_variable_node(self, contents):
    5555        return DebugVariableNode(contents)
    5656
    57     def extend_nodelist(self, nodelist, node, token):
    58         node.source = token.source
    59         super(DebugParser, self).extend_nodelist(nodelist, node, token)
    60 
    6157    def unclosed_block_tag(self, parse_until):
    6258        command, source = self.command_stack.pop()
    6359        msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until))
  • template/loader.py

     
    7878    def reload(self):
    7979        return self.loader(self.loadname, self.dirs)[0]
    8080
     81
     82class LoaderOriginLite(object):
     83
     84    def __init__(self, display_name, loader, name, dirs):
     85        self.name = display_name
     86        self.loader, self.loadname, self.dirs = loader, name, dirs
     87
     88
    8189def make_origin(display_name, loader, name, dirs):
    8290    if settings.TEMPLATE_DEBUG and display_name:
    8391        return LoaderOrigin(display_name, loader, name, dirs)
    8492    else:
    85         return None
     93        return LoaderOriginLite(display_name, loader, name, dirs)
    8694
    8795def find_template_loader(loader):
    8896    if isinstance(loader, (tuple, list)):
     
    117125    else:
    118126        raise ImproperlyConfigured('Loader does not define a "load_template" callable template source loader')
    119127
    120 def find_template(name, dirs=None):
     128
     129def find_template(name, dirs=None, skip_template=None):
    121130    # Calculate template_source_loaders the first time the function is executed
    122131    # because putting this logic in the module-level namespace may cause
    123132    # circular import errors. See Django ticket #1292.
     
    129138            if loader is not None:
    130139                loaders.append(loader)
    131140        template_source_loaders = tuple(loaders)
     141    template_candidate = None
    132142    for loader in template_source_loaders:
    133143        try:
    134144            source, display_name = loader(name, dirs)
    135             return (source, make_origin(display_name, loader, name, dirs))
     145            if skip_template:
     146                extends_tags = source.nodelist[0]
     147                extends_tags_origin, extends_tags_source = extends_tags.source
     148                if extends_tags_origin.name == skip_template:
     149                    template_candidate = None
     150                    continue
     151                if not template_candidate:
     152                    template_candidate = (source, make_origin(display_name, loader, name, dirs))
     153            else:
     154                return (source, make_origin(display_name, loader, name, dirs))
    136155        except TemplateDoesNotExist:
    137156            pass
    138     raise TemplateDoesNotExist(name)
     157    if not template_candidate:
     158        raise TemplateDoesNotExist(name)
     159    return template_candidate
    139160
    140161def find_template_source(name, dirs=None):
    141162    # For backward compatibility
     
    149170        raise Exception("Found a compiled template that is incompatible with the deprecated `django.template.loaders.find_template_source` function.")
    150171    return template, origin
    151172
    152 def get_template(template_name):
     173def get_template(template_name, skip_template=None):
    153174    """
    154175    Returns a compiled Template object for the given template name,
    155176    handling template inheritance recursively.
    156177    """
    157     template, origin = find_template(template_name)
     178    template, origin = find_template(template_name, skip_template=skip_template)
    158179    if not hasattr(template, 'render'):
    159180        # template needs to be compiled
    160181        template = get_template_from_string(template, origin, template_name)
Back to Top