Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation with dynamic variables for Twig symfony

Tags:

php

twig

symfony

I have a specific problem with concat of twig. When I trying to concatenate dynamic variables showing the error. Here is my code :

{% set i = 0 %}
{% set nbLignes = codeEvt.nb_lignes_~i %}
{% set nbLignesRef = codeEvt.nb_lignes_ref_~i %}

But I have this error message :

Method "nb_lignes_" for object "\DTO\SuiviJourFonc" does not exist in XXXXXXXXX.html.twig at line 211

I would like to take codeEvt.nb_lignes_0 , but i would like build a "for" for others variables like nb_lignes_1, nb_lignes_2 , nb_lignes_3 ...

How can i do this ?

like image 359
alexandre bru Avatar asked Nov 24 '25 18:11

alexandre bru


1 Answers

attribute can be used to access a dynamic attribute of a variable: The attribute function was added in Twig 1.2.

{{ attribute(object, method) }}

{{ attribute(object, method,arguments) }}

{{ attribute(array, item) }}

Try like this,

{{ attribute(codeEvt, 'nb_lignes_ref_' ~ i) }}
like image 173
Nisam Avatar answered Nov 27 '25 07:11

Nisam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!