Search code examples
symfonytwigtwig-extension

Nested variables in Twig


I have a problem with twig. I need to use nested variables such as (account is an object)

{{ likes_array.[account.idOnSn].lifetime }}

This is a result of print_r(likes_array)

array(2) {
    ["tanitweb"]=> array(2) {
        ["lifetime"]=> int(1420)
        ["daily"]=> int(0)
    }
    ["fstjuniorentreprise"]=> array(2) {
        ["lifetime"]=> int(3310)
        ["daily"]=> int(3310)
    }
}

I get this exception

Expected name or number in StatsDotTnMainBundle:Default:acceuil.php.twig at line 188


Solution

  • Use index notation, with square brackets:

    {{ likes_array[account.idOnSn].lifetime }}