Opened 5 years ago

Closed 5 years ago

#30671 closed New feature (wontfix)

Conditional for Template Block Content.

Reported by: Novomotus Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords: block, conditional, extends
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When extending templates, it would be convenient to allow conditionals for determining whether or not a block has any data being passed.

Example:

base.html

<h1>{% block title %}{% endblock title %}</h1>
<h2>{% block subtitle %}{% endblock subtitle %}</h1>

When extending via another template, there may not always be both title and subtitle elements required for rendering:

article.html

{% extends '/templates/base.html' %}

<article class="article">
    {% block title %}{{ article.title }}{% endblock title %}
    {% block subtitle %}{{ article.subtitle }}{% endblock subtitle %}
    <p class="article-body">{{article.body }}</p>
</article>

Note: the second examples assumes a context object 'article' with properties 'title', 'subtitle', and 'body'.

Issue Case

In the above example, consider that an 'article' context object may have a title but no subtitle.

There would be an empty <h2></h2> element rendered as the "subtitle" propery is wrapped in those html tags inside the "base.html" parent template.

Requested Functionality

Below is a syntactically-fictitious example of the proposed functionality:

base.html
{% if block|subtitle%}
<h1>{% block title %}{% endblock title %}</h1>
{% endblock subtitle %}

Where the

{% if block|subtitle %}

statement would check for the existence of data of a block named "subtitle"

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: wontfix
Status: newclosed
Summary: Conditional for Template Block ContentConditional for Template Block Content.
Version: 2.2master

Thanks for this proposition, however I'm not sure if it's feasible, I don't see a wide usage, and IMO this can be solved with different tags. I would use include instead of block and extend, e.g.

titles.html

{% if obj.title and obj.subtitle %}
    <h1>{{ obj.title }}</h1>
    <h2>{{ obj.subtitle }}</h2>
{% endif %}
article.html

<article class="article">
    {% include "titles.html" with obj=article %}
    <p class="article-body">{{ article.body }}</p>
</article>

Even if it's not a proper solution for you I am convinced that you can solve your issue with existing tags, please use one of support channels.

Note: See TracTickets for help on using tickets.
Back to Top