[Django] #30539: Django - “with / as” to fill block is not working

3 views
Skip to first unread message

Django

unread,
Jun 3, 2019, 1:05:59 AM6/3/19
to django-...@googlegroups.com
#30539: Django - “with / as” to fill block is not working
-------------------------------------+-------------------------------------
Reporter: Jay-Dai | Owner: nobody
Type: Bug | Status: new
Component: Template | Version: 2.2
system | Keywords: templates, blocks,
Severity: Normal | with
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 1 |
-------------------------------------+-------------------------------------
I am trying to not repeat myself in Django, but use the same content for 2
blocks. The content is static so I would rather have in in my templates
than sending them through views.

This was the solution I found :


{{{
{% with "My title" as title %}
{% block TitleOne %}{{ title }}{% endblock TitleOne %}
{% block TitleTwo %}{{ title }}{% endblock TitleTwo %}
{% endwith %}
}}}


In a templates that extends a second one that uses the blocks TitleOne and
TitleTwo .

But it does not work. If I write it like :


{{{
{% block TitleOne %}"My title"{% endblock TitleOne %}
{% block TitleTwo %}"My title"{% endblock TitleTwo %}
}}}


It works perfectly. But of course it s not DRY. If I write it like :


{{{
{% with "My title" as title %}
{% block TitleOne %}"My title"{% endblock TitleOne %}
{% block TitleTwo %}{{ title }}{% endblock TitleTwo %}
{% endwith %}
}}}


Only the 1st one displays right. But not DRY as well.

Tried another way suggested in Django's docs :
https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#with

{{{
{% with title="My title" %}
{% block TitleOne %}"My title"{% endblock TitleOne %}
{% block TitleTwo %}{{ title }}{% endblock TitleTwo %}
{% endwith %}
}}}

Only the 1st one displayed too..

(I am using Django 2.2.1, Python 3.7.3)

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

Django

unread,
Jun 3, 2019, 1:49:41 AM6/3/19
to django-...@googlegroups.com
#30539: Django - “with / as” to fill block is not working.
-------------------------------------+-------------------------------------
Reporter: Jay Day | Owner: nobody
Type: Bug | Status: closed
Component: Template system | Version: master
Severity: Normal | Resolution:
| worksforme
Keywords: templates, blocks, | Triage Stage:
with | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* ui_ux: 1 => 0
* resolution: => worksforme
* version: 2.2 => master


Comment:

Thanks for the report, however I cannot reproduce your issue. In all
described cases everything works for me.

--
Ticket URL: <https://code.djangoproject.com/ticket/30539#comment:1>

Reply all
Reply to author
Forward
0 new messages