If you're writing a node template, you can access all of the node's fields in a couple of different ways.
Nodes store their field values in two places: {{ content }}
and {{ node }}
. Individual field values can be available at either node.field_name
or content.field_name
. Here's when to use which:
content.field_name
This is for fields which contain content that you want to render on the page. The content
variable only contains those fields which are active in the display settings of that content type. When you use {{ content }}
in your twig template, all the active fields will automatically render in the order set in the content type's display settings. If you want to render a specific field separately in a specific place (for example, maybe you want to render a hero image field individually) you can use {{ content.field_name }}
to render it. Then when you render the rest of the content just use {{ content|without('field_name') }}
.
node.field_name
This is for fields which are not displayed on the page, but are instead being used to control other things. All of the node's fields are available in {{ node }}
whether they are active or not. So for example, you can set up a boolean toggle field and hide it in the content type's display settings, yet still access it as a conditional in your twig templates using {{ node.field_name }}
.
In templates.php, you can assign variables to $variables['whatever']...
node
for a node, $variables['whatever'] become $whatever under the node template
page
for a page, $variables['whatever'] becomes $page['whatever']
$variables
built by drupal during a page request. all the modules have the ability to add to and alter these. the last thing that happens before rendering is the theme_alter and theme_preprocess functions fire, giving a theme a last change to change these
$GLOBAL
a set of variables that once defined will live throughout the rest of the process, and be available to everything. be careful -- name conflicts and memory badness are a problem here!!! Also makes for hard-to-debug code