Description

For an app we're building with Django, it's become a requirement to be able to vary the template load directories based on the request information we get from the user, including the directories read by the extends and includes TemplateTags. The attached patch, along with the one on #4278 gives us all the functionality we need.

There are some comments on the PR for improvement. I also have a couple more thoughts:

I'm not super enthusiastic about this feature, perhaps partly because the use case is a bit vague -- I'd like to hear more about apps that allow choosing template directories based on user inputs.

Do you know if Jinja2 offers a feature like this? I ask because there has been some talk of switching Django to use Jinja2 rather than using our own template language.

I'd also like to see a test to ensure that directory traverse isn't possible.

The current implementation doesn't seem ideal as it forces the context variable to be called dirs (am I reading that correctly?). Thus, if I have two {% include %} tags and want different directories for each of them, I'd have to jump through some hoops to make that work (like {% with dirs1 as dirs %}{% include "foo.html" dirs %}{% endwith %}) ...

This patch breaks the TEMPLATE_LOADERS abstraction: it's adding in the template language a feature that alters the template loading behavior, but only works with Django's filesystem template loader — not the app directories template loader, for instance.

This ticket may predate the introduction of TEMPLATE_LOADERS. In my opinion it should be closed as wontfix, and the reporter should write a custom template loader. It's going to be a bit difficult to pass the request to the template loader; it may require a global (thread-local) variable or hooking on signal.