Jinja: Block assignment scope is different than inline assignment

Created on 27 Jun 2016  路  5Comments  路  Source: pallets/jinja

I want to set a variable in a child template that will be rendered in the parent template. If I use block assignment, nothing is rendered. If I use inline assignment, it works as expected. I am using Jinja 2.8 with Python 2.

base.html:

{{ content }}

bad.html with block assignment, nothing rendered:

{% extends 'base.html' %}
{% set content %}
Hello, World!
{% endset %}

good.html, renders correctly:

{% extends 'base.html' %}
{% set content = 'Hello, World!' %}
bug

Most helpful comment

Got this problem too. Simple test:

{% extends "..." %}
...
{% set aaa = 'test' %}
{% set bbb %}
    <a href="/">bar</a>
{% endset %}
...
{% block some_block %}
 {{ aaa }} {# renders 'test' #}
 {{ bbb }} {# renders empty string #}
{% endblock %}

All 5 comments

Got this problem too. Simple test:

{% extends "..." %}
...
{% set aaa = 'test' %}
{% set bbb %}
    <a href="/">bar</a>
{% endset %}
...
{% block some_block %}
 {{ aaa }} {# renders 'test' #}
 {{ bbb }} {# renders empty string #}
{% endblock %}

I'm getting the same inconsistent behavior with the 'with' statment:

{% with %}
{% set something = 'werd' %}
{% set something_block %}
werd werd werd
{% endset %}
{% endwith %}
...
{{ something }} {# renders empty string #}
{{ something_block }} {# renders 'werd werd werd' #}

these bugs are probably related?

The with statement is not supposed to work that way, so that's intentional. I cannot reproduce the set issue however independently of this in master.

Nevermind, I see what this issue is about.

Changed in 45b59b522a542c21611214b352df049a3b6dbcb4

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guettli picture guettli  路  5Comments

DriverX picture DriverX  路  4Comments

glasserc picture glasserc  路  4Comments

harobed picture harobed  路  6Comments

The-Compiler picture The-Compiler  路  4Comments