Symfony2/Twig - how to set template file contents as variable?

Viewed 414

In Symfony 2.8 I've got SharedBundle, where I keep views and other things I share between other bundles, including the whole layout that should be extended by other bundles' views.
That shared layout has some typical blocks, from which sidebar block should be different for different bundles.

Currently I did an ugly workaround:

In SharedBundle sidebar container:

{% if sidebarcontent is defined %}
    {{ sidebarcontent|raw }}
{% else %}
    SIDEBAR NOT FOUND
{% endif %}

And in other bundles (here: BundleA) in every view that extends the shared main view:

{% extends 'SharedBundle:layout.html.twig' %}
{% block sidebar %}
    {% include 'BundleA:_partials:sidebar.html.twig' %}
{% endblock %}
{% set sidebarcontent =  block('sidebar') %}

But that doesn't look good, I think. Is there a better way?

1 Answers
Related