I have twig variables inside an array called social_media that contain colors. When I perform {{ dump(social_media) }} I can see all the information like so

array(10) {
["facebook"]=> string(39) "https://www.facebook.com/myfacebook/"
["facebook-color"]=> string(7) "#2d5f9d"
["twitter"]=> string(33) "https://twitter.com/mytwitter"
["twitter-color”] => string(7) "#51acef"
["youtube"]=> string(56) "https://www.youtube.com/channel/myyoutube"
["youtube-color"]=> string(7) "#e52d27"
["instagram"]=> string(35) "https://instagram.com/myinstagram"
["instagram-color"]=> string(7) "#f77737"
["linkedin"]=> string(83) "https://www.linkedin.com/company/mylinkedin"
["linkedin-color"]=> string(7) "#0077b5" 

Performing {{ social_media.facebook }} works fine like so


but {{ social_media.facebook-color }} outputs


In fact every single color variable outputs this same value, while the others work fine. I considered the possibility that the # was causing a problem, but changing the colors to colors like 'Black' or 'Red' as a test, but I received the same outcome. I even tried using

{{ social_media.facebook-color|raw }}

But that did not have any effect. What could possibly cause some of my twig variables to only output int(0) while others output just fine?


It turns out that twig can't handle hyphens in their variable names. Changing the variable names from

{{ social_media.facebook-color }}


{{ social_media.facebook_color }}

resolved the issue

  • 2
    Twig uses the same variable naming scheme as PHP, so hyphens are interpreted as subtraction (thus the int(0)). You can still use hyphens if you access the variable using the Twig attribute function: attribute(social_media, 'facebook-color')
    – mikeker
    Feb 15 '17 at 19:43

Your Answer

