Get all nodes of a specific content type and print the data of each node inside page.html.twig I have a content type (Flower) with two fields: Name and Color.

In my-theme/templates/page.html.twig, I want to print the name and the color of each flower.

&lt;div id="page"&gt;
    {% for flower in flowers %}
        &lt;div class="flower-item"&gt;
            &lt;h3&gt;{{ [Flower Name Here] }}&lt;/h3&gt;
            &lt;p&gt;{{ [Flower Color Here] }}&lt;/p&gt;
        &lt;/div&gt;
    {% endfor %}
&lt;/div&gt;


How can I get all nodes of a given content type and print the values of each node inside page.html.twig?
Putting this in page.html.twig is a bad idea. Since this template will be used for every page rendered from Drupal using that theme.

Instead you should simply create a View. Views can be used to query a certain content type and to list nodes in a certain view mode or just certain fields of these nodes (the title and a color field for example). Then configure your view to provide a block and place that block in the region you wanna have the Flowers printed.

Views and block will take care of the caching. And you can use the block's visibility settings to have it displayed only on paths or nodes where you really need it.



If you still insist on coding this in a template (not recommended, too performance-heavy, too much logic in templates) you first have to pass all Flower nodes to the template.

/**
 * Implements template_preprocess_page().
 */
function MYTHEME_preprocess_page(&amp;$variables) {

  $query = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;getQuery();

  // Get all Flower node IDs.
  $nids = $query-&gt;condition('type', 'flower')-&gt;execute();

  // Load all Flower nodes.
  $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

  // Pass them to page.html.twig.
  $variables['flowers'] = $nodes;
}


Then in your page.html.twig:

{% for flower in flowers %}
  {{ flower.title.value }}
  {{ flower.field_color.value }}
{% endfor %}