Symfony - Twig - Dynamic variable - How to concatenate two variables in a loop

Zagloo

I have to concatenate two variables in one, beside the request.locale

I explain to you :

I have an Entity named Lexicon with several field: wordFr, wordEn, definitionFr, definitionEn

I tried to do something like that for replace the Fr or En according to the request.locale but it doesn't work :

             {% set locale = '' %}

             {% if app.request.locale == "fr" %}
                 {% set locale = 'Fr' %}
             {% else %}
                 {% set locale = 'En' %}
             {% endif %}

             {% for wordList in wordsList %}
                 <tr>
                     <td>{{ wordList.word~locale }}</td>
                     <td>{{ wordList.definition~locale }}</td>
                 </tr>
             {% endfor %}

How to have {{ wordList.wordFr }} or {{ wordList.wordEn }} according to the locale (replace var locale by Fr or En) ? thanks !

In the meanwhile I did this but it's too long and repetitive...

                {% if app.request.locale == "fr" %}
                    {% for listeMots in listeMotsLexique %}
                        <tr>
                            <td>{{ wordList.wordFr }}</td>
                            <td>{{ wordList.definitionFr }}</td>
                        </tr>
                    {% endfor %}
                {% else %}
                    {% for listeMots in listeMotsLexique %}
                        <tr>
                            <td>{{ wordList.wordEn }}</td>
                            <td>{{ wordList.definitionEn }}</td>
                        </tr>
                    {% endfor %}
                {% endif %}
Jessy Amyot

What you want is to use the Twig attribute function which is documented here.

It allows you to use dynamic variable names. You would have to do something like that:

{{ attribute(wordList, 'mot'~locale) }}

You're basically saying that you want the 'mot'~locale from the wordList object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to concatenate two Twig variables in a loop

From Dev

How to concatenate two Twig variables in a loop

From Dev

Concatenation with dynamic variables for Twig symfony

From Dev

how to concatenate variables into a variable with java

From Dev

Twig: concatenate variable in string

From Dev

How to use global variable in symfony twig file?

From Dev

Symfony Twig global variables

From Java

How to concatenate strings in twig

From Dev

How to display img inside twig with variable + string concatenate?

From Dev

How shall i concatenate two or more values/variables to a new variable in laravel

From Dev

How to concatenate a backslash between two variables?

From Dev

concatenate string with integer to use as a variable (creating dynamic variables) in jquery

From Dev

twig dynamic variable call

From Dev

How to Concatenate variable name in a vba loop?

From Dev

symfony twig render dynamic twig code

From Dev

symfony twig render dynamic twig code

From Dev

Twig Variable Variables for a method

From Dev

How use rendered variables in a foreach Symfony 2 ~ twig

From Dev

How to set global variables in Twig using symfony2

From Dev

How use rendered variables in a foreach Symfony 2 ~ twig

From Dev

Twig Variable Scope in Loop

From Dev

how to use two variables in for loop?

From Dev

Symfony2: How to replace a certain string with a global Twig variable?

From Dev

How to render TWIG output to a variable for later use (symfony2)?

From Dev

Concatenate a variable within a loop

From Dev

Symfony variable does not exist in twig

From Dev

How to Loop over different type variables, randomize and concatenate them?

From Dev

How to add Dynamic Scope variables to a for loop in angularjs?

From Dev

how make these variables dynamic in foreach loop

Related Related

  1. 1

    How to concatenate two Twig variables in a loop

  2. 2

    How to concatenate two Twig variables in a loop

  3. 3

    Concatenation with dynamic variables for Twig symfony

  4. 4

    how to concatenate variables into a variable with java

  5. 5

    Twig: concatenate variable in string

  6. 6

    How to use global variable in symfony twig file?

  7. 7

    Symfony Twig global variables

  8. 8

    How to concatenate strings in twig

  9. 9

    How to display img inside twig with variable + string concatenate?

  10. 10

    How shall i concatenate two or more values/variables to a new variable in laravel

  11. 11

    How to concatenate a backslash between two variables?

  12. 12

    concatenate string with integer to use as a variable (creating dynamic variables) in jquery

  13. 13

    twig dynamic variable call

  14. 14

    How to Concatenate variable name in a vba loop?

  15. 15

    symfony twig render dynamic twig code

  16. 16

    symfony twig render dynamic twig code

  17. 17

    Twig Variable Variables for a method

  18. 18

    How use rendered variables in a foreach Symfony 2 ~ twig

  19. 19

    How to set global variables in Twig using symfony2

  20. 20

    How use rendered variables in a foreach Symfony 2 ~ twig

  21. 21

    Twig Variable Scope in Loop

  22. 22

    how to use two variables in for loop?

  23. 23

    Symfony2: How to replace a certain string with a global Twig variable?

  24. 24

    How to render TWIG output to a variable for later use (symfony2)?

  25. 25

    Concatenate a variable within a loop

  26. 26

    Symfony variable does not exist in twig

  27. 27

    How to Loop over different type variables, randomize and concatenate them?

  28. 28

    How to add Dynamic Scope variables to a for loop in angularjs?

  29. 29

    how make these variables dynamic in foreach loop

HotTag

Archive