Reputation: 10028
I have parent template where settings variable and this template includes couple of other sub-templates where I want to reuse this variable. Unfortunately in children templates this variable is empty. How to fix it?
<!-- index.html -->
{%- set title= ' :: '.join((caption, page_title or '')) -%}
{%- set description= ' :: '.join((desc, meta_desc or '')) -%}
{%- block page_header -%}
{% include 'parts/_header.html' %}
<!-- I also tried do it with context without luck -->
{% include 'parts/_header.html' %}
{%- endblock page_header -%}
<!-- parts/header.html -->
<header class="header-wrapper">
<div class="header">
<div class="title"><h1 class="title">{{ title|safe }}</h1></div>
<div class="description">{{ description|safe }}</div>
</div>
</header>
UPD: After small research I found that issue related to {% block %}
section - in include
tag is outside block
then it is provided with document context. But for some reasons I'd like to keep it inside block
.
Upvotes: 1
Views: 1922
Reputation: 759
In my experience, this can be fixed by including your {% set %}
declarations inside the {% block %}
declarations. In your example:
<!-- index.html -->
{%- block page_header -%}
{%- set title= ' :: '.join((caption, page_title or '')) -%}
{%- set description= ' :: '.join((desc, meta_desc or '')) -%}
{% include 'parts/_header.html' %}
{%- endblock page_header -%}
If you need to set the same variables outside the block, I would just put the same line twice, once outside the block and once inside. This seems imperfect to me, but I don't know another way.
Upvotes: 2