简体   繁体   中英

Name of Twig variable in a Twig variable

I get a list of collections right from Doctrine and I store them into an array. For example:

$data['collList'] = $heyDoctrine->giveMeMyColls();

But I also want to retrieve some informations about these collections. I store it into $data['collectionId'] . Until this point, everything works fine. But in my Twig template, I want to create ordered lists with the name of my collection and every item of this list would be an information about this collection. So, in PHP, I would do this:

foreach($data['collList'] as $collItem){
    echo $collItem['name'];
    echo '<ul>';
    foreach($data[$collItem['id']] as $collItemData){
        echo '<li>'.$collItemData.'</li>';
    }
}

My problem is: how to do this with Twig? I don't know how to say to Twig «hey, use «coll.id» as THE NAME of an other variable! I've looked a bit and I've found the «attribute» function, but I wasn't able to make it work.

How should I do that? Thanks a lot.

So, try next twig code:

{% for key, collItem in data.collList %}
    {{ collItem.name }}
    <ul>
        {% for collItemData in data[collItem.key] if key == 'id' %}
            <li> {{ collItemData }} </li>
        {% endfor %}
    </ul>
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM