Ticket #9154: templates_optimizations_r9076.diff

File templates_optimizations_r9076.diff, 4.3 KB (added by Manuel Saelices, 16 years ago)

Templates system optimization

  • django/contrib/admin/templatetags/log.py

     
    1515            context[self.varname] = LogEntry.objects.all().select_related('content_type', 'user')[:self.limit]
    1616        else:
    1717            if not self.user.isdigit():
    18                 self.user = context[self.user].id
    19             context[self.varname] = LogEntry.objects.filter(user__id__exact=self.user).select_related('content_type', 'user')[:self.limit]
     18                user_id = context[self.user].id
     19            context[self.varname] = LogEntry.objects.filter(user__id__exact=user_id).select_related('content_type', 'user')[:self.limit]
    2020        return ''
    2121
    2222class DoGetAdminLog:
  • django/template/loader_tags.py

     
    11from django.template import TemplateSyntaxError, TemplateDoesNotExist, Variable
    22from django.template import Library, Node, TextNode
    3 from django.template.loader import get_template, get_template_from_string, find_template_source
     3from django.template.loader import get_template
    44from django.conf import settings
    55from django.utils.safestring import mark_safe
    66
     
    4343        self.nodelist = nodelist
    4444        self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
    4545        self.template_dirs = template_dirs
     46        self.compiled_parent = self.get_parent()
    4647
    4748    def __repr__(self):
    4849        if self.parent_name_expr:
    4950            return "<ExtendsNode: extends %s>" % self.parent_name_expr.token
    5051        return '<ExtendsNode: extends "%s">' % self.parent_name
    5152
    52     def get_parent(self, context):
    53         if self.parent_name_expr:
    54             self.parent_name = self.parent_name_expr.resolve(context)
     53    def get_parent(self):
    5554        parent = self.parent_name
    5655        if not parent:
    5756            error_msg = "Invalid template name in 'extends' tag: %r." % parent
     
    6160        if hasattr(parent, 'render'):
    6261            return parent # parent is a Template object
    6362        try:
    64             source, origin = find_template_source(parent, self.template_dirs)
     63            return get_template(parent, self.template_dirs)
    6564        except TemplateDoesNotExist:
    6665            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
    67         else:
    68             return get_template_from_string(source, origin, parent)
    6966
    7067    def render(self, context):
    71         compiled_parent = self.get_parent(context)
     68        compiled_parent = self.compiled_parent
    7269        parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
    7370        for block_node in self.nodelist.get_nodes_by_type(BlockNode):
    7471            # Check for a BlockNode with this node's name, and replace it if found.
  • django/template/loader.py

     
    2222
    2323from django.core.exceptions import ImproperlyConfigured
    2424from django.template import Origin, Template, Context, TemplateDoesNotExist, add_to_builtins
     25from django.utils.functional import memoize
    2526from django.conf import settings
    2627
    2728template_source_loaders = None
    2829
     30_template_cache = {}
     31
    2932class LoaderOrigin(Origin):
    3033    def __init__(self, display_name, loader, name, dirs):
    3134        super(LoaderOrigin, self).__init__(display_name)
     
    7275            pass
    7376    raise TemplateDoesNotExist, name
    7477
    75 def get_template(template_name):
     78def get_template(template_name, dirs=None):
    7679    """
    7780    Returns a compiled Template object for the given template name,
    7881    handling template inheritance recursively.
    7982    """
    80     source, origin = find_template_source(template_name)
     83    if template_name in _template_cache:
     84        return _template_cache[template_name]
     85    source, origin = find_template_source(template_name, dirs)
    8186    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