Opened 10 years ago

Closed 10 years ago

#24232 closed New feature (wontfix)

Add conditional expressions to block tag

Reported by: Markus Amalthea Magnuson Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It would be nice if I could add a conditional expression after a block name, mimicking the shortcut conditional expression form in Python, to only render a block if a certain condition is true. So instead of writing e.g. this:

{% block title %}
  {% if post.title %}
    {{ post.title }}
  {% else %}
    {{ block.super }}
  {% endif %}
{% endblock %}

I could write this:

{% block title if post.title %}My title{% endif%}

See also #9173 (comment 9) where I first thought of this.

I have some working proof of concept code and could open a pull request to start working on this, if accepted.

Change History (3)

comment:1 by Tim Graham, 10 years ago

How is this different from #9173? I suggest to write to the DevelopersMailingList to get feedback on your proposal.

comment:2 by Markus Amalthea Magnuson, 10 years ago

#9173 is for rendering a block only if a child template also defines that block and has some content in it.

This ticket is for the more general usage of rendering a block only if some condition is true. We cannot currently put {% block %} tags inside {% if %} tags, and this would solve that.

(Or rather, we can do that, but the block will always be rendered.)

Last edited 10 years ago by Markus Amalthea Magnuson (previous) (diff)

comment:3 by Tim Graham, 10 years ago

Resolution: wontfix
Status: newclosed

Closing this particular solution based on the mailing list discussion, although it seems like there might be some improvement in this area that could be done.
https://groups.google.com/d/msg/django-developers/myj8aEcVMt4/0bAITiexTCcJ

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