7

I am working on my template and have gotten it laid out almost how I want.

I have it print the content with {{ page.content }} but what I need is on another part of the page I want to print out a specific field from that same content.

I tried different combinations of twig tags but none worked for me.

{{ page.content.field_name }}
{{ content.field_name }}
{{ item.field_name }}
{{ field_name }}
and a few others

I tried googling but I didn't find anything.

1
  • Suggestion: enable twig debug and write {{ dump() }}
    – Eyal
    Dec 3, 2015 at 19:49

2 Answers 2

6

Consult this, you will see there is a variable node that is fully loaded node object. so try the following. one of them should work

{{ node.field_name.value }}
{{ node.field_name }}
{{ content.field_name }}
10
  • Thanks, when I tried that I get the error "Exception: Object of type Drupal\Core\Field\FieldItemList cannot be printed" Dec 3, 2015 at 15:32
  • try in with | raw filter much or _toString
    – Shabir A.
    Dec 3, 2015 at 15:55
  • Thanks for the continued help | raw gives me the same error and __toString makes the page work but still doesn't print the field Dec 3, 2015 at 15:58
  • is your field a single valued or multivalued?
    – Shabir A.
    Dec 3, 2015 at 16:04
  • 5
    This works: {{ node.field_name.value }}
    – 4uk4
    Dec 3, 2015 at 16:10
3

If you use Twig Tweak you can just use {{ node.field_name|view }} for anything (taxonomy terms, paragraphs, etc.) and it will render consistently based on whatever field display settings you have for a given field.

See the bottom item of the Twig Tweak Cheat Sheet for an example (and lots of other super-helpful stuff you can do with Twig Tweak).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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