0

I have an array in twig named mark which contains classes. Here is an extract:

array(7) {
  [0]=>
     object(stdClass)#526 (11) {
        ["Algebre"]=>
        string(2) "11"
        ["ElectroCinetics"]=>
        string(2) "16"
        ["Statistics"]=>
        string(1) "8"
        ["French"]=>
        string(1) "9"
        ["Commerce_Int"]=>
        string(2) "14"
        ["Total"]=>
        string(5) "11.23"
        ["name"]=>
        string(16) "Jonh Doe"
    }

 

Now the point is I can't know the name of the keys because they are added dynamically by admin. All I can do is get them from database and store them into a variable called module. SO i have something like this:

{%set module="Electrocinetics" %}

And so when I want to display the mark I just do: Mark:{{mark.module}}

and nothing is displayed because twig thinks the key it should seek for in the array is named module. But I want it to read the content of the variable rather. Is there a way to force twig to read the content of the variable instead?

Bandoss
  • 11
  • 2

1 Answers1

0

You could use attribute function.
https://twig.symfony.com/doc/3.x/functions/attribute.html

Like this:

{%set module="Electrocinetics" %}

Mark:{{attribute(mark, module)}}

Playground example:
https://twigfiddle.com/1digwh

Related post:
Accessing array values using array key from Twig

Bsalex
  • 2,847
  • 1
  • 15
  • 22