user1830833
user1830833

Reputation: 181

Twig pass an include inside of an include

I want to pass an include as part of a variable passed in another include. Let me paint a picture:

{% include '@AppBundle/P1/template.html.twig' with {
            'body': '<div class="col-xs-12">
                    <h1>Dummy Title</h1>
                    include '@AppBundle/P1/Form/dummyForm.html.twig' with {'form': test_form }
                    '</div>'
            }%}

Is this at all possible? I experimented with escaping the strings and concatenations but I always get "punctuation" expected with value "," 404. I really am curious if anyone has encountered such problem and if so, how they overcame it. Thank you all!

Upvotes: 0

Views: 1217

Answers (1)

Alain
Alain

Reputation: 36954

Hummm, to answer your question first, you can use the function instead of the tag:

main.twig

{{ 
    include('@AppBundle/P1/template.html.twig', {
        'body': include('@AppBundle/P1/dummyBody.html.twig')
    })
}}

dummyBody.html.twig

<div class="col-xs-12">
    <h1>Dummy Title</h1>
    {{ include('@AppBundle/P1/Form/dummyForm.html.twig', {'form': test_form }) }}
</div>

Demo on twigfiddle


But you should be aware of blocks, those are made for this!

base.twig

<div class="col-xs-12">
    <h1>Dummy Title</h1>
    {% block form %}{% endblock %}
</div>

form.twig

{% extends 'base.twig' %}

{% block form %}
   some form
{% endblock %}

Rendering form.twig will display the same thing, but cleaner and easily reusable.

Demo on twigfiddle

Upvotes: 1

Related Questions