Opened 5 years ago
Closed 5 years ago
#31915 closed New feature (wontfix)
Allow load template tag before extends tag
| Reported by: | Jann Haber | Owned by: | nobody |
|---|---|---|---|
| Component: | Template system | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Currently the extends template tag always needs to be the first template tag used in a template (if it is used at all). In the extends tag, it is possible to use the built-in template tags. For example, with Django 3.0, we have been using different base templates depending on if a request was an ajax request or not:
{% extends request.is_ajax|yesno:"base_ajax.html,base.html" %}
Once the is_ajax method is removed from the request objects, this is not possible anymore. A new template tag for "is_ajax" could be created (like {% extends request|is_ajax|yesno... %} ), but it is not possible to load it before extends, since extends always needs to be the first template tag. The only possible workaround I see to achieve the same behaviour, is to create a context processor, however this will be processed for all requests, even the ones that don't need this feature.
All in all, I am requesting to allow load template tags before "extends".
I didn't test but you should be able to set the
builtinsoption in your DTL settingshttps://docs.djangoproject.com/en/3.1/topics/templates/#django.template.backends.django.DjangoTemplates
Failing that, you'd need to move the
is_ajaxlogic into your view.