symfony2 - Twig: Configure parent template from child?
layout, symfony, twig
Solution
You can use twig block inheritance for that:
{# layout.html.twig #}
<div id="main" class="{% block column_layout 'single-column' %}">
{% block content %}{% endblock %}
</div>
Then in your other template, if you want to change the class of your div#main, just reimplement the block
{% extends "layout.html.twig" %}
{% block column_layout 'two-column' %}
{% block content %}
content here
{% endblock %}
PD: `{% block name content %}` is a shortcut syntax for blocks with little content
Problem
I'm new to sf2/twig and I think I'm missing a point somewhere: I have a layout with a header, two-column middle part and footer. The middle part has the main content area on the left, and a 33% sidebar on the right. I use twig's template inheritance to put my content into the main area and where otherwise required. Now, there are some sections of our page where the content area must take up the whole width, so the sidebar on the right isn't displayed for these sections. To achieve this, I have to apply a different CSS class to the main content area and, obviously, don't render the sidebar's content. It would be cool if I could use a variable in the main template like `{% if single_column_layout %}`, with a default value of `false`, which I could overwrite in the child template when needed. But it feels like I'm heading in the wrong direction with this... In a more general way, on a page made of a bunch of building blocks, how would one best control which blocks are displayed and which are not, per page?