Differentiate placeholder and content provider blocks

The block is a core feature in the template system. Currently, the syntax provides no explicit grammar to differentiate between a placeholder and a content provider. It would be nice to have a way to mark a block as a content provider and differentiate it from a placeholder. This would enhance readability, and open the door to other possibilities. Here is an example to illustrate the proposed syntax:

{% block usertools = %} my user tools {% endblock %}

Note the equal sign in the end of the block opening tag, which is an element added here to indicate that this is a content provider for the block named ‘usertools’ defined somewhere else. The ‘=’ sign is analogous to that in an assignment statement in most programming languages, since a content provider block can be seen as assigning a value or content to a block. Viewed in this way, hopefully the syntax now looks more natural and meaningful. If one would like to append some extra content to this block, it can be done as follows:

{% block usertools += %} additional user tools {% endblock %}

Besides enhancing readability, I think there can be a lot of other benefits with this syntax, such as having a block accumulate contents gradually based on different conditions:

{% block usertools = %} my user tools {% endblock %}
{% if user.authorized %}
{% block usertools += %}, extra user tools {% endblock %}
{% endif %}

In this illustration, if user.authorized is False, then the block content is simply ‘my user tools’, otherwise, it would be ‘my user tools, extra user tools’. This would make the block content specification more flexible and convenient.

It may open the door for other possibilities with this syntax. For example, multiple occurrences of such content providers for the same block can exist in a single template file, which was requested several times in the past.

It would be super nice to hear what the community thinks about this, thanks a lot.

<opinion>

I believe this is confusing the difference between the block tag (used to identify segments overriding blocks in templates being extended), and the includes tag, in which is used to pull in template segments from a different file.

I have found that, for me, designing templates in Django is different than any other web framework / template system I have encountered in the past. It took me a while to “get it”.

So no, my opinion is that I don’t see any benefit to what you’re proposing. If perhaps you posted a more complete and real life example where your suggestion is the only way to accomplish something specific, this could change. But at this time, I’m unaware of a case where it would be useful / needed.

For the one specific case where you mention replicating blocks, generally, I see that as a sign that you should be looking at using an include, rather than a block.

</opinion>

I think this is a bad idea.

At first glance, this opinion seems reasonable, but it ultimately makes it difficult for users to understand the code. It also makes the code more complex.

In addition, alternative solutions to solve this problem already exist.

If you need to use one block in multiple templates, you can solve the problem by creating as many blocks as necessary or loading another template using the {% include %} tag.

Thank you both for sharing your thoughts about this idea. Django template system is surely very powerful, and has many good things in it. But the syntax around the block is somewhat counterintuitive. I believe this proposal can lead to fixing those related issues. But I understand that many people have already got used to such counterintuitiveness, it is probably too late to fix.