4

I have two templates (and two corresponding preprocess hooks), one rendering a Paragraph, one rendering items from this paragraph.

How to access a variable available in the parent paragraph template (or preprocess Hook) from the items rendering template (or item preprocess hook) ?

paragraph--paragraph-row.html.twig

{{ content.MyVARIABLE }}  //it works

field--paragraph--paragraph-row2.html.twig

<div class="row">
  {% for item in items %}
     <div class="col-sm-{{ 12 // parent.MyVARIABLE  }}">
         {{ item }}
    </div>
{% endfor %}
</div>

THANKS !

David

2
  • parent. is a bit confusing here, for a field the parent is the entity the field is placed in, possible a paragraph for nested paragraphs, but can be any entity. Or do you mean the paragraphs stored in the field, more like children then?
    – 4uk4
    Jul 27, 2017 at 10:11
  • "parent" means I want to access the paragraph the field is nested in.
    – deivh
    Jul 27, 2017 at 10:22

1 Answer 1

2

You can't access easily variables from other templates. But you can use the entity objects to access field values.

For a nested paragraph you find the parent paragraph in element['#object']:

field--paragraph.html.twig

{{ element['#object'].field_my_field.value }}
1
  • This is a really important information. Where can it be found in the documentation?
    – 0711master
    Feb 14, 2018 at 12:04

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.