Support a new `{% set %}` tag and defining dicts, list, sets and tuples in `{% with %}` and `{% set %}`

Precisely! Why not such a tags already exists in DTL? I see people reimplement this again and again, why let people constantly reinvent the wheel?

Or better: allow people to create common Python iterables in templates. Really I don’t understand the opposition other than pure ideology. Since the starting of this discussion, I have seen any good reason to not let people create dicts in DTL other that “it’s like that and ti’s always been like this”. And it’s honestly getting frustrating.

Yes, the whole discussion is frustrating I grant you. The DTL has a specific philosophy that is made clear — to keep programming construct out of template, which leads to cleaner, more testable, more maintainable code — and yet again and again and again, without accepting the answers given, folks keep asking “Can we turn it into a half-baked Jinja?”

Surely it’s not so hard to see why that solution suits no-one?

The tags maintained in Django itself cover more than the 90% use case. We don’t add everything because there’s no end to that. It become unmaintainable. Adding tags to your own project is no problem. Share them if you think they’re truly useful.

If you want actual advice, try leaning into the DTL’s way of doing things. The water is nice. You might find you like it.

I’m going to unsubscribe from this thread now.

1 Like

Or you can turn underscores into dashes for arbitrary kwargs, as my heroicons package does: GitHub - adamchainz/heroicons: Use heroicons in your Django and Jinja templates.


I’m also dropping out of the conversation here. I think there’s enough of a consensus against that we don’t need further discussion.

I agree in principle with you that DTL are weirdly crippled. But I don’t really see the big use of creating iterables/dicts specifically.

Both those examples you gave are about under powered form libraries not being able to configure attributes, which is imo better solved by better and higher abstractions (like iommi’s attrs feature for forms, tables, fragments, etc).

Just out of curiosity, here, what’s the rationale behind also not accepting whitespace controls like {%- -%} ?

1 Like

I don’t understand the question. What is that?

https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control

Would spaceless work for you or is not precise enough? Built-in template tags and filters | Django documentation | Django

Unfortunately, {% spaceless %} is a massive destruction weapon. It remove each and every blank line, which is not what you want when formatting pure text. I ended-up writing my own tag for this which was uselessly tedious and produces highly difficult to read templates where -%} would have been way lighter.

In my experience {% spaceless %} always does something no one expects (insert monty python joke here). I keep forgetting what it does and trying to use it only to be surprised and reverting the change. I can’t even remember what it does not except that it’s wrong/useless (for every case I’ve heard about).

I flinched when I saw that syntax. That’s pretty obscure and hard to read!