title: Conditional Inheritance in Twig
body: <p>I have <strong>different layouts</strong> depending on the user. This triggers the following error:
  <code>"Multiple extends tags are forbidden"</code>. How can I manage to use different layouts depending on the role of the user?</p>
  
  <pre><code>{% if is_granted('ROLE_USER_ONE') %}
  
  {% extends "AcmeUserBundle::layout_user_one.html.twig" %}
  
  {% elseif is_granted('ROLE_USER_TWO') %}
  
  {% extends "AcmeUserBundle::layout_user_two.html.twig" %}
  
  {% endif %}
  </code></pre>
  
  <h2>EDIT</h2>
  
  <p>Here is the answer. I will use the case of 3 users in case people wonder how to do this. In this case <code>admin</code> has also <code>userOne</code> and <code>userTwo</code> privileges in case someone wonders about the <code>else</code> statement. I use <code>Conditional Inheritance</code> in this case, but as suggested in one of the answer, <code>Dynamic Inheritance</code> might be more readable.</p>
  
  <pre><code>{% set admin = false %}
  
  {% set userOne = false %}
  
  {% set userTwo = false %}
  
  {% if is_granted('ROLE_ADMIN') %}
  
      {% set admin = true %}
  
  {% else %}
  
      {% if is_granted('ROLE_USER_ONE') %}
  
          {% set userOne = true %}
  
      {% elseif is_granted('ROLE_USER_TWO') %}
  
          {% set userTwo = true %}
  
      {% endif %}
  
  {% endif %}
  
  {% extends admin ? "AcmeUserBundle::layout_admin.html.twig" : userTwo ? "AcmeUserBundle::layout_user_two.html.twig" : "AcmeUserBundle::layout_user_one.html.twig" %}
  </code></pre>