On 03/06/11 17:36, Aymeric Augustin wrote:

> ConstantIncludeNode improves performance because it calls
> django.template.loader.get_template() only once in __init__() and not
> in each call to render().
> 
> get_template() invokes the template loading machinery to create a
> compiled Template object, given a template path. If it's a
> performance bottleneck, we can memoize its results. That will improve
> performance of all template lookup operations, not only {% include
> %}.

We already have the cached template loader, and we don't want to invoke
that kind of behaviour unless asked for (it's up to users to put it in
their TEMPLATE_LOADERS settings), as it has significant memory overhead.

I was thinking something simpler, like this:

class IncludeNode(BaseIncludeNode):
    def __init__(self, template_name, *args, **kwargs):
        super(IncludeNode, self).__init__(*args, **kwargs)
        self.template_name = template_name

    def render(self, context):
        try:
            template_name = self.template_name.resolve(context)
            if getattr(self, 'last_template_name', None) == \
                    template_name:
                template = self.template
            else:
                template = get_template(template_name)
                self.template = template
                self.last_template_name = template_name
            return self.render_template(template, context)
        except:
            if settings.TEMPLATE_DEBUG:
                raise
            return ''

If I'm thinking correctly, that will keep the included template in
memory only for the lifetime of the parent template, and will avoid
loading it more than once if it is a static string and the include is in
a loop.

Luke

-- 
"Underachievement: The tallest blade of grass is the first to be
cut by the lawnmower." (despair.com)

Luke Plant || http://lukeplant.me.uk/

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to