How to correctly pass a variable from a twig include file to another include twig file

275 Views Asked by At

I am trying to pass a variable from one twig include file to another twig include file, (I did post earlier but my question probably wasn't as clear as it could have been). I have 3 files, the view.twig & then a gallery.twig & galleryConfig.twig files

I somehow need to extract the variables from the galleryConfig.twig file and have these available to the gallery.twig file - I know how to pass a variable directly into the template using the with - but how would I use this to pass ALL the variables from galleryConfig.twig into the gallery.twig file

view.twig

<div>
  {% include '@app/components/view/images/galleryConfig.twig' %}
  {% include '@app/components/view/images/gallery.twig' with {
      'holdingImage' : 'somefile.png'
  } %}       
</div>

galleryConfig.twig

{% block javascript %}
    <script type="text/javascript">
          
    </script>
{% endblock %}
{% set holdingImage = ('holding-image.svg') %}

gallery.twig

{% block content %}
<div>
  {{ holdingImage }}
</div>
{% endblock %}

Expected result : The gallery.twig view to display the holdingImage variable which is holding-image.svg and stored in the galleryConfig.twig file

0

There are 0 best solutions below