Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Twig variable inside an include and use it afterward

imagine you have a twig template (tplA.twig) that include an other one

<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 the 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 master template? How to use globals? Note that I can't use blocks and extends, and I'm not using Symfony.

Many thanks

EDIT

The real context is a very basic multilingual structure. For a page, I have one master 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>

It is a very small website, so I didn't created a complex structure with database, and I wanted to manage all this stuff in the template.

The thing is: how is it possible to display the right localized strings? What I thought about was to include a localized template, to separate concerns. Something like:

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

And inside this template, I would have setted all the variables in the right locale, so I could use them in my photos list. (as explained in the first part of my question)

note that I have this structure for all others pages. Text content is separated in the locale folder, and each page has a master template (again, it's a very basic personal website)

What I did finally was to insert a hudge if statement before the photos list. If locale is fr, set variables with french texts, if it's en, set variables with english texts.

That does the trick, so I'm ok with that ^^

like image 903
rekam Avatar asked Oct 17 '14 10:10

rekam


People also ask

How do you dump variables in Twig?

Using the Manage administrative menu, navigate to Configuration > Development > Devel settings (admin/config/development/devel) and under the heading, Variables Dumper, select Kint, then Save configuration. Like the dump function, kint() will not display any output unless debugging is enabled.

How do I add a global variable in Twig?

If you are using Twig in another project, you can set your globals directly in the environment: $twig = new Twig_Environment($loader); $twig->addGlobal('myStuff', $someVariable); And then use {{ myStuff }} anywhere in your application.


1 Answers

Why don't you move the second call of {{ test }} in tplB ( for this occasion ) ?

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

like image 134
Georgi Georgiev Avatar answered Nov 14 '22 23:11

Georgi Georgiev