hank
hank

Reputation: 137

Referencing paragraphs content in Twig file

I have a page template twig, where I would like to populate a div with contents with a paragraph field from the node contents. So I have paragraph field, which may contain multiple different paragraph types, so people can build (as far as paragraph types allow) a column like they want. So far so good.

But the problem arises as I haven't been able to reference the paragraph type, whatever I have tried. I'd just like to splash the paragraph contents with their respective paragraph twigs to the respective div.

My first attempt was simple one: {{ node.field_section_a.value }} (with and without raw) nothing Then I googled around and found that I'd need to push entity, so {{ node.field_section_a.entity.value }} (with and without raw) nothing I also have tried (to get even SOMETHING out) to insert .0. to just reference the first element with all afore-mentioned permutations.

Actually, I found article at https://gist.github.com/frankyonnetti/39ef8984acfca879d16d8a4890095bb9, which allows me now to enumerate the paragraphs like this:

{% for i, paragraph_entry in node.field_section_a %}
   {{ paragraph_entry.entity.value }}
{% endfor %}

In the article the fields of paragraph were dissected into the twig, but this doesn't work here, as I need to be able to use whatever paragrah. I just wanted to show the rendered entity of the paragraph. Now, what remains is what to write inside those double curved braces... ;)

Upvotes: 0

Views: 4397

Answers (1)

hank
hank

Reputation: 137

Ok, I got it working after finding the bamboo_twig-module.

So all I needed after installing the module and enabling Bamboo Twig - Loaders is to add

{{ bamboo_render_field('field_section_a', 'node') }}

into the right place in the page.html.twig.

Thanks https://www.drupal.org/forum/support/theme-development/2017-06-02/how-to-print-paragraph-in-pagehtmltwig for solving this.

Upvotes: 1

Related Questions