Represents a variable in a twig using for loop

In my service, I am returning an array that looks like this:

$array = [
    'bible_anagram_word'    => $result->getBibleAnagramWord(),
    'word_1'                => $result->getWord1(),
    'word_2'                => $result->getWord2(),
    'word_3'                => $result->getWord3(),
    ...
    'word_22'               => $result->getWord22(),
    'word_23'               => $result->getWord23(),
    'word_24'               => $result->getWord24(),
    'word_25'               => $result->getWord25(),
    'Bible_verse_reference' => $result->getBibleVerseReference(),
    'Bible_verse_text'      => $result->getBibleVerseText(),
    'Bible_translation'     => $result->getBibleTranslation(),
];

      

How do I represent document.word _ ## in this loop for ?

{% for i in 1..25 %}
    {{ document.word_ ~ i|slice(0,1) }}
{% endfor %}

      

In PHP, this code:

substr ( $this->document['word_'.$i] , 0 , 1 )

      

My failed attempts include:

  • {{document.word_ ~ i | slice (0,1)}}
  • {{(document.word_ ~ i) | slice (0,1)}}
  • {{'document.word_ ~ i' | slice (0,1)}}
+3


source to share


1 answer


This should do the trick

{{ attribute(document, 'word_' ~ i) }} 

      



More on attribute function

+6


source







All Articles