1

I have a template that extends the page.html.twig base template. I wish to render a field variable from the node as a heading in other parts of the page template.

I've tried {{dump(page.content)}} but the variable does not seem to be there, it is already rendered into HTML.

What's the syntax - if it exists - for accessing a node variable from the page.content variable?

For example, at the node level, I can access {{ content.field_heading }}.

At the page level, what can I use? ({{ page.content.??? }})

1 Answer 1

3

Per the docs on the core page.html.twig:

 \* Page content (in order of occurrence in the default page.html.twig):
 \* - messages: Status and error messages. Should be displayed prominently.
 \* - node: Fully loaded node, if there is an automatically-loaded node
 \*   associated with the page and the node ID is the second argument in the
 \*   page's path (e.g. node/12345 and node/12345/revisions, but not
 \*   comment/reply/12345).

The node variable is available in page.html.twig. If you need to access one its fields you can use {{ node.field_myfield.value }} to print it out.

3
  • 2
    Correct but note that this does not include the rendered fields that are available as content in the node template. You can only access the raw values. You can use the view() API method on the fields to prepare rendered fields yourself or you can use an entity view block, e.g. provided by the ctools module to display some fields somewhere in a block.
    – Berdir
    Commented Aug 7, 2016 at 7:35
  • When I try print out a field consisting of a taxonomy term: eg {{ node.field_tax_term.value }} I get a user error: User error: "target_id" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php). If I print it out on the node the same field works fine. Do taxonomy terms behave differently?
    – tanbog
    Commented Aug 13, 2016 at 6:10
  • I still do not have a working solution for this. Shawn Conn's answer does not work all the time and often triggers the error I outlined above.
    – tanbog
    Commented Sep 16, 2016 at 3:54

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.