In Drupal 7, I can do this in a custom field template to print the alt tag of an image:

<?php print $item['#item']['alt']; ?>

And though "item" seems to be a variable in Twig / drupal 8 fields, I can't seem to do the same thing. I've tried these:

{{ item.alt }}
{{ item.item.alt }}

These don't work but I don't get any errors either. Looking at the API does not give me much insight. I saw this gist but again, no alt tag rendered separately. My image is rendering the alt tag as i can see it in the code though.


2 Answers 2


The syntax to print the alt tag in that case is this:

{{ item['#item'].alt }}

Twig in Drupal 8 doesn't deal with hash keys like #item in render arrays cleanly. Part of the problem is that # signifies a comment in Twig, so {{ item.#item.alt }} wouldn't work.

There is the possibility of working around this with a Twig extension to allow drilling down into hash attributes with the syntax {{ item.item.alt }} but I'm not sure if anyone is working on that. I should mention that Twig extensions can be added in contrib as well.

  • Thanks, that works great. I could not find any documentation in regard to this, is there any reference you can point me to? Commented Dec 20, 2013 at 15:08
  • 2
    Things like this should probably be added under drupal.org/theme-guide/8 when they are specific to Twig + Drupal. I can see this potentially fitting in to drupal.org/node/1918824, maybe under Variables?
    – star-szr
    Commented Dec 20, 2013 at 15:18
  • 1
    See also drupal.org/node/2160611 :)
    – star-szr
    Commented Dec 20, 2013 at 15:54
  • 1
    I added this example to drupal.org/node/1918824, as suggested by @Cottser, and added a comment pointing to the core issue that he filed.
    – Ursula
    Commented Dec 20, 2013 at 18:52
  • On Drupal 8 {{ item['#item'].alt }} doesn’t work for me. Instead, other solution by @Hugo {{ item.content['#item'].alt }} is ok. Commented Jun 13, 2018 at 12:28

I achieved printing the alt attribute of my field using this line {{ item.content['#item'].alt }}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.