That newlines inside {% %} is not allowed makes some formatting of templates more ugly and bad for git blame. It also causes problems for beginners, as the error message when you do this is not great.
currently renders {% %} as a string in the template, while it would be an error with this change.
Maybe this needs to be an option on the template settings, with a deprecation warning if you haven’t turned it on, and change startproject to insert that setting by default.
Yes please! Especially with scoped includes, for me they can get very long and very hard to read:
{% include "client/visitorcount/store_visitorcount_overall_statistics.html" with overall_statistics=overall_statistics overall_statistics_json=overall_statistics_json selected_store=selected_store user_stores_with_sensors=user_stores_with_sensors chart_height=weeks_comparison_chart_height only %}
Would rather have this look like:
{%
include
"client/visitorcount/store_visitorcount_overall_statistics.html"
with
chart_height=weeks_comparison_chart_height
overall_statistics_json=overall_statistics_json
overall_statistics=overall_statistics
selected_store=selected_store
user_stores_with_sensors=user_stores_with_sensors
only
%}
+1 – thank you for finding that thread and tagging me, I’d still love that, just ran out of steam last time I tried to make it happen. There wasn’t any particular reason, just got busy with other things.
django-upgrade isn’t in the templates game, but Djade is
We could do this to “escape” any existing multiline non-tags:
-{% This isn't meant to be a tag
+{{ '{' }}% This isn't meant to be a tag
-nor this %}
+nor this %{{ '}' }}
Happy to add this to Djade, if need be.
But then that doesn’t really fix adoption.
Perhaps a better approach would be a Python-style “future load” to initially opt into the multiline tag feature: {% load multiline from future %}. We can flag the deprecation warning for any templates that contain unmatched {% / {{ / {#. Then a later version makes the behaviour default.
I’m also for some sort of tooling that would detect that during the upgrade process but if django-upgrade is not that tool, I’m not for asking people to run X different tools to upgrade something
I also have been wanting this for many years. At work we use a lot of includes and other tags with multiple often quite long strings passed in (sometimes template names and so).
I think there’s clear rationale and consensus for it and a PR that could hopefully be revived, so I think someone needs to step up and write the ticket and open a new PR.
It doesn’t need to be part-way through. Like Python’s __future__ rules, we can require that the special tag appears near the top of the file.
The change here is in tokenization, which is technically the step before parsing.
The warning needs only be done once when loading the template. After tokenizing, when the old mode is in effect, we could check all text nodes for {%, {{, or {#, and warn for each occurrence. I would expect this to be a small cost compared to the existing tokenization run.
The “templates are slow” statement/meme normally refers to render time, not load/parsing time, as far as I’ve heard. So, I don’t think such a check would impact the main cost.