简体   繁体   中英

How to include twig within twig

I need help in including in a twig file, another twig file

For example, I have:
_cdn/widgets/filter/filter.php
_cdn/widgets/filter/view/filter.twig

And:
themes/tema02/view/search.twig
themes/tema02/search.php

I need to include the filter.twig in the search.twig
But when I use include, it does not render the filter.php
And it gets a lot of 'Undefined variable:'

So how do I do this?

First of all show your code . It's always easier to give right anwser when you see the code.

Use {% include %} for including twig in twig

https://twig.symfony.com/doc/2.x/tags/include.html

Based on your description you need to pass requred variables to included twig , something like this

{% include 'filter.twig' with {'foo': 'bar'} %}

OR

In filter twig you can put if ... is defined where you try to access variable

{% if foo is defined %}   
    code where you doing something  with  foo variable 
{% endif %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM