#35636 closed New feature (wontfix)
differentiate placeholder and content provider blocks
Reported by: | Yingjie Lan | Owned by: | Hashir Hassan |
---|---|---|---|
Component: | Template system | Version: | 5.0 |
Severity: | Normal | Keywords: | template block |
Cc: | Yingjie Lan | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
The block is a core feature in the template system. Currently, the syntax provides no explicit grammar to differentiate between a placeholder block and a content provider block. In this ticket, an optional and fully backward compatible syntax is proposed to mark a block as a content provider. This new syntax enhances readability, and imposes little effort to implement, and opens the door to allow multiple occurrences of the same block in a single template being given different contents on different conditions (this feature has been requested many times over the years).
Here is the proposed syntax illustrated with an example:
{% block usertools = %} here are my user tools. {% endblock %}
Note the equal sign in the end of the block opening mark, which is an optional element added here to indicate that this is not a placeholder, but a content provider for the block named 'usertools' defined somewhere else.
If this syntax is implemented (e.g. by using a boolean property of the block object), the multiple occurrence of such content providers in a single template file can be allowed in the future if deemed desirable.
Change History (4)
comment:1 by , 5 months ago
Description: | modified (diff) |
---|
comment:2 by , 5 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:3 by , 5 months ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
comment:4 by , 5 months ago
Hi Sarah Boyce, thanks a lot for your feedback! I have initiated a new topic on the Django Forum. I am sorry that the proposed syntax looks confusing to you, I should have explained it with more clarity. 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. 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.
Hi Yingjie Lan, we recommend proposing new features on the Django Forum, where you'll reach a wider audience and likely get extra feedback. You need to build consensus that the community is in favor of this change.
Personally, I'm not in favor of doing this. I think the syntax is confusing (looks like a typo) and the benefits are limited (you can comment in a template if you want to add extra information).
But if you find there is a community agreement for this, you are welcome to come back to the ticket and point to the forum topic, so we can then re-open it. For more details, please see the documented guidelines for requesting features.