I am creating a custom template for my theme and I am constructing a page.html.twig page. I want to call of the individual node fields into separate rows. I have been able to successfully display everything except the paragraph field.

I have tried.

{{ node.field_benefits }}
{{ node.field_benefits.value }}
{{ paragraph.field_benefits }}
{{ paragraph.field_benefits.value }}
{{ node.paragraphs.field_benefits }}

And nothing has worked. Does anyone have any idea of the correct structure to call in the paragraph module?

Or would a for function work

{% if node.field_benefits %}
{% for field_items in node.field_benefits %}
{{ field_item.value }}
{% endfor %}
{% endif %}
  • page.html.twig doesn't handle nodes... only the overall page template. You are using the wrong template. I suggest you override the node template instead (or at least read up on the way twig templates are accessed). Also, turning on twig debug and looking at the source code will help you ascertain which template you should be overriding. – NikLP Feb 26 '16 at 20:08
  • Ok @NikLP I moved everything to the node level, but I am still unable to render the paragraphs module - I am using the field that I have obtained from debugging and kint and still it shows an empty row – Megan McGowan Feb 26 '16 at 22:22
  • Maybe check this out? drupal.org/node/2444893 – NikLP Feb 29 '16 at 11:29

First add a paragraph--your_paragraph_machine_name.html.twig to the templates directory of your theme.

Accessing your paragraph fields can be done by adding twig variables in a format similar to {{ content.field_your_field }}.

If you want only the field value, that's a little trickier. For instance if you had a List (text) field and wanted to get the value from the selected option, you could do something like:

{% for item in content.field_select_list %}
  {% if item is iterable %}
    {% for key,value in item %}
      {% if not value is iterable %}
        {% if key == '#markup' %}
          {{ value }}
        {% endif %}
      {% endif %}
    {% endfor %}
  {% endif %}
{% endfor %}

You can always run a {{ dump(content) }} to see the structure and get the values you're looking for. Hope this helps!

EDIT: You can avoid all the silly nested loops. The above example should be written like:

{% set value = content.field_select_list.0['#markup'] %}
{{ value }}

For a Text (plain) field it might look something like:

{{ content.field_text.0['#context'].value }}

To get a URL value from a Link field I've seen suggestions like:

{{ content.field_link['#items'].0.value.uri }}

But it would have an "internal:/" prefix, so I've had better luck with something like this:

{{ content.field_link.0['#title'] %}
  • You can use paragraph.field_my_text.value in any paragraph template to output the plain, unformated value, just like node.field_my_text.value in a node template. – Philipp Michael May 15 '17 at 8:05
  • how do you print paragraph--your_paragraph_machine_name.html.twig from your main template? – Sam Nov 4 '18 at 16:32

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.