0

I have a template for a specific content type. I want to output individual fields from that content type in a specific order, which requires me to change the content type template. It's too complicated to explain why I need to do this.

In the template I see {{ page.content }} for outputting all the fields together, which displays everything, but I want to take the field variables out of this, and separately display each of them.

So for example I tried the following, but it displays nothing to the page.

{{ page.content.field_field_name1 }}
{{ page.content.field_field_name2 }}

I also tried {{ content.field_field_name1 }}, {{ field_field_name1 }}, and {{ content.field_field_name1.entity }}, but none of them work.

Suggestions?

6
  • For one, content is the render array. So it should be {{ content.field_name }} in the template. If the cache is cleared and nothing shows, then the fields may not be displaying in the view mode display settings on that content type.
    – Kevin
    Jul 24, 2017 at 20:38
  • Ok...that makes sense. I haven't edited for View Mode Display for that content type however. When I go to the content type Manage Display tab I only see 'teaser' checked under custom...but I'm using the teaser, I'm viewing the full content. All the fields are listed above the custom display settings.
    – Sage
    Jul 24, 2017 at 20:56
  • You can't view the full content and use another view mode on that route. It will always use default ( or full if checked)
    – Kevin
    Jul 24, 2017 at 20:58
  • Sorry, what? Route? I tried checking both full content and teaser, and unchecking both and saving both as checked and unchecked...nothing changes. nothing is displayed unless i change my twig template to {{ page.content }} then it's all displayed again.
    – Sage
    Jul 24, 2017 at 21:01
  • @Sage Are you using the correct machine name? I see you're using the word "field" twice in your twig {{ content.field_field_name1 }}. Does the word "field" show twice in manage fields? You should be using it like this {{ content.machine_name }} So ex: {{ content.field_ingredients }}
    – No Sssweat
    Jul 24, 2017 at 21:50

1 Answer 1

3

You can not print out content fields directly in the page template. content is available in the node template, not in the page template.

The correct way to print out parts of the node elswhere in the page template is to define a block that does that, for example with the ctools module and a specific view mode that just contains the fields you want.

Alternatively, you can load the node yourself in theme_preprocess_page() and then view/prepare them manually and put it in $variables. But using blocks and regions is the standard and recommended way to put something in the page template.

0

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.