Opened 14 years ago
Closed 14 years ago
#17553 closed Uncategorized (needsinfo)
Modify render_to_string to pass TEMPLATE_LOADERS the context_instance arguments
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Template system | Version: | 1.3 |
| Severity: | Normal | Keywords: | template loaders |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I propose that render_to_string be modified to pass context_instance all the way down to TEMPLATE_LOADERS.
This change would allow template loaders to dynamically select a template according to the context. For example, to instrument an A/B test by checking the request in a context or to return a mobile template to a mobile user agent:
from django.template.loaders.filesystem import load_template_source as default_template_loader
def mobile_loader(template_name, template_dirs=None, context_instance=None):
if context_instance is not None:
if context_instance['mobile']:
template_name = 'm' + template_name
return default_template_loader(template_name, template_dirs, context_instance)
At the render_to_string level, this would allow you to change the rendering of most pluggable apps as well.
Note:
See TracTickets
for help on using tickets.
That's already pretty easy to do with the TemplateResponse introduced in Django 1.3: https://docs.djangoproject.com/en/dev/releases/1.3/#templateresponse
I'm not sure why render_to_string specifically needs that ability, would you care to elaborate?