> How can I cache the sub-template witch is included in the main
> template? And how can I ensure it won't be render(parse) the next
> request?

If you look at django.template.loader_tags.do_include you'll notice an
if statement that checks if the given argument was a string. If so it'll
use a ConstantIncludeNode rather than a normal IncludeNode. The constant
include node inlines the sub-template while the normal node does have to
look up that variable during render time.

Side notice: That's also the reason why you can't include templates
recursively with static strings but have to use a workaround like "{%
with "recursive.html" as template %}{% include template %}{% endwith %}"
or a custom template tag.


Caching the template is as easy as storing the result from get_template
in a global variable.

e.g.

from django.template.loader import get_template
foo_template = get_template('foobar.html')
def foo(request):
    ctx = RequestContext(request)
    ctx.update({'foo': True, 'bar': False})
    return HttpResponse(foo_template.render(ctx))


Note: I'd rather lazy load the template in order to decouple things a
bit. Be aware of race conditions and better synchronize the template
loading. The above code snippet is only meant to give you the idea.


--mp

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

Reply via email to