Re: [Django] #9173: Conditional content of template blocks

18 views
Skip to first unread message

Django

unread,
May 22, 2011, 6:21:51 AM5/22/11
to django-...@googlegroups.com
#9173: Conditional content of template blocks
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
while0pass | Status: new
Type: New | Component: Template system
feature | Severity: Normal
Milestone: | Keywords:
Version: SVN | Has patch: 1
Resolution: | Needs tests: 0
Triage Stage: Design | Easy pickings: 0
decision needed |
Needs documentation: 0 |
Patch needs improvement: 0 |
-------------------------------------+-------------------------------------
Changes (by adurdin):

* has_patch: 0 => 1
* version: 1.0 => SVN
* easy: => 0


Comment:

This scratches an itch for me too, so I thought I’d implement it.

My patch in `ticket9173.diff` (against r16253) implements a new tag,
'''ifnotempty''', with the following behaviour:

{{{
Renders only if at least one variable or template tag within also
renders.

This allows a variable or template tag to be surrounded by markup,
with
the markup omitted if the variable or template tag does not render.

An `{% else %}` block may also be used.

In the following example, the section will not be rendered unless
another
template extends this one and overrides the `more_information` block::

{% ifnotempty %}
<section>
<h1>More information</h1>
{% block more_information %}{% endblock %}
</section>
{% endifnotempty %}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/9173#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 24, 2012, 10:26:14 AM5/24/12
to django-...@googlegroups.com
#9173: Conditional content of template blocks
-------------------------------------+-------------------------------------
Reporter: while0pass | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Design
Has patch: 1 | decision needed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by masterjakul@…):

* cc: masterjakul@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/9173#comment:6>

Django

unread,
Apr 4, 2013, 6:30:53 PM4/4/13
to django-...@googlegroups.com
#9173: Conditional content of template blocks
---------------------------------+-----------------------------------------

Reporter: while0pass | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------
Changes (by jacob):

* stage: Design decision needed => Someday/Maybe


Comment:

I'm wholly unconvinced by the syntax proposed so far; it seems quite
complicated and non-intuitive. However, I'm not opposed to the idea in
general. So marking "someday" -- if someone comes up with syntax that
makes sense and is easy to explain, then we might consider this.

--
Ticket URL: <https://code.djangoproject.com/ticket/9173#comment:7>

Django

unread,
Aug 26, 2013, 8:51:06 PM8/26/13
to django-...@googlegroups.com
#9173: Conditional content of template blocks
---------------------------------+-----------------------------------------
Reporter: while0pass | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------
Changes (by FunkyBob):

* cc: FunkyBob (added)


Comment:

I ended up implementing a {% wrapif %} tag, which will render the
[optional] head and tail blocks if the body renders as a non-blank [not
all whitespace] string.

{{{
{% wrapif %}
<td>
{% body %}
{% block whatever %}{% endblock %}
{% tail %}
</td>
{% endwrapif %}
}}}

If body is omitted, the first block is assumed to be the body, and the
head empty.
If tail is omitted, it is considered empty.


I mostly did this to help move container tags out of for loops, where
they'd be inside a {% if forloop.first %} and thus cause a test every
iteration.

--
Ticket URL: <https://code.djangoproject.com/ticket/9173#comment:8>

Django

unread,
Jan 12, 2015, 4:48:07 PM1/12/15
to django-...@googlegroups.com
#9173: Conditional content of template blocks
---------------------------------+-----------------------------------------
Reporter: while0pass | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------

Comment (by alimony):

I was thinking about this the other day. How about something like:

{{{
parent.html

<table>
<tr>
<td>{% block firstcol %}{% endblock %}</td>
<td>{% block secondcol %}{% endblock %}</td>
{% block thirdcol if block.child %}<td>{{ block.child }}</td>{%
endblock %}
</tr>
</table>
}}}

Which would introduce a `{{ block.child }}` thing similar to `{{
block.super }}`

And also conditional blocks in general, in which you can specify:

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

Which is the equivalent of, but shorter than:

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

What do you think of all the above?

--
Ticket URL: <https://code.djangoproject.com/ticket/9173#comment:9>

Django

unread,
Jan 9, 2019, 4:40:59 PM1/9/19
to django-...@googlegroups.com
#9173: Conditional content of template blocks
---------------------------------+-----------------------------------------
Reporter: while0pass | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------
Changes (by Collin Anderson):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/9173#comment:10>

Reply all
Reply to author
Forward
0 new messages