Set the Twig variable inside include and use it later

Imagine you have a branch template (tplA.twig) that includes another

<p>Twig template A</p>

{% set test = "in tpl A" %}
<p>first, variable is: {{ test }}</p>

{% include "tplB.twig" %}
<p>and the variable is: {{ test }}</p>

and included template (tplB.twig)

<div>Content of tpl B</div>
{% set test = "now is tpl B" %}

What is the best way to set / change a variable in an included template and use it in the main template? How to use global variables? Please note: I cannot use blocks and extend, and I do not use Symfony.

Many thanks

EDIT

The real context is a very simple multilingual structure. For the page, I have one main template:

<h1>{{ i18n('mainTitle') }}</h1>
<h2>current language (fr, en): {{ ln }}</h2>

<!-- here a list of photos, but not in a foreach loop -->
<div>
    <div>
        <img src="/same/for/all/languages-a.jpg" alt="localized A" />
        <span>localized A</span>
    </div>
    <div>
        <img src="/same/for/all/languages-b.jpg" alt="localized B" />
        <span>localized B</span>
    </div>
    <!-- etc. -->
</div>

This is a very small website, so I did not create a complex database structure, and I wanted to manage all this material in a template.

, ? , , . - :

<!-- before the photos list -->
{% include ln ~ '/photos-list.twig' %}
<!-- then the photos list -->

, . ( )

, . , ( , )

hudge if . locale - fr, , en, .

, ^^

+4
1

{{ test }} tplB ( )?

<div>Content of tpl B</div> {% set test = "now is tpl B" %} <p>and the variable is: {{ test }}</p>

0

All Articles