Ticket #3544: 3544.contexttemplatecache.diff

File 3544.contexttemplatecache.diff, 2.6 KB (added by Johannes Dollinger, 16 years ago)
  • django/template/context.py

     
    1313        dict_ = dict_ or {}
    1414        self.dicts = [dict_]
    1515        self.autoescape = autoescape
     16        self.template_cache = {}
    1617
    1718    def __repr__(self):
    1819        return repr(self.dicts)
     
    6465        "Like dict.update(). Pushes an entire dictionary's keys and values onto the context."
    6566        self.dicts = [other_dict] + self.dicts
    6667        return other_dict
     68       
     69    def get_template(self, template_name):
     70        if not template_name in self.template_cache:
     71            from django.template.loader import get_template
     72            self.template_cache[template_name] = get_template(template_name)
     73        return self.template_cache[template_name]
    6774
    6875# This is a function rather than module-level procedural code because we only
    6976# want it to execute if somebody uses RequestContext.
  • django/template/loader_tags.py

     
    9292                parent_block.nodelist = block_node.nodelist
    9393        return compiled_parent.render(context)
    9494
    95 class ConstantIncludeNode(Node):
    96     def __init__(self, template_path):
    97         try:
    98             t = get_template(template_path)
    99             self.template = t
    100         except:
    101             if settings.TEMPLATE_DEBUG:
    102                 raise
    103             self.template = None
    104 
    105     def render(self, context):
    106         if self.template:
    107             return self.template.render(context)
    108         else:
    109             return ''
    110 
    11195class IncludeNode(Node):
    11296    def __init__(self, template_name):
    11397        self.template_name = Variable(template_name)
     
    11599    def render(self, context):
    116100        try:
    117101            template_name = self.template_name.resolve(context)
    118             t = get_template(template_name)
     102            t = context.get_template(template_name)
    119103            return t.render(context)
    120104        except TemplateSyntaxError, e:
    121105            if settings.TEMPLATE_DEBUG:
     
    178162    bits = token.contents.split()
    179163    if len(bits) != 2:
    180164        raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0]
    181     path = bits[1]
    182     if path[0] in ('"', "'") and path[-1] == path[0]:
    183         return ConstantIncludeNode(path[1:-1])
    184165    return IncludeNode(bits[1])
    185166
    186167register.tag('block', do_block)
Back to Top