How can I conditionally override a TWIG layout block?

13,915

Solution 1

Blocks don't care about any logic around it, as said in the documentation:

A block provides a way to change how a certain part of a template is rendered but it does not interfere in any way with the logic around it.

You should put that logic inside the block, not on the outerside, as you can see on the last example in that article.

Solution 2

Define

{% block footer %}Some standard content{% endblock %}

in parent twig template. Then in template where you want to decide if display content of footer you can do:

{% block footer %}
  {% if not modal == true %}
    {{ parent() }}
  {% endif %}
{% endblock %}

If the modal is true - footer will be empty, if not - in footer will be printed "Some standard content"

Share:
13,915

Related videos on Youtube

slave2zeros
Author by

slave2zeros

Updated on June 07, 2022

Comments

  • slave2zeros
    slave2zeros almost 2 years

    First, let me start with the code I'm attempting to use:

    {% if modal == true %}
        {% block header %}{% endblock %}
        {% block footer %}{% endblock %}
    {% endif %}
    

    What I'm trying to accomplish is to not show my header and footer blocks ONLY if the variable called modal is true. I also have this below the if statement:

    {% block content %}
        {{ dump(modal) }}
    {% endblock %}
    

    What happens here is that my override for emptying the header and footer blocks always runs regardless of if the value of modal is true or otherwise. So, I run this with modal passed in as false and the result is that the header and footer still don't show. The output of the dump command accurately shows true or false, but the condition always seems to evaluate to true in the if statement.

    Can blocks not be wrapped in a conditional statement, or is there something additional I need to do to make this work?

    Thanks for any help you can offer.

  • slave2zeros
    slave2zeros over 11 years
    Thanks. I was just talking to a coworker who suggested the same thing. He said to try rendering the parent block if modal is not true.
  • AJ Cerqueti
    AJ Cerqueti about 10 years
    Whilst accepted answer is correct, this answer takes that approach and shows how to actually implement that solution. +1 for {{ parent() }}
  • numediaweb
    numediaweb over 8 years
    This is the best answer as it falls back to defaults
  • Auxiliary Joel
    Auxiliary Joel about 5 years
    is there any other tips or documentation that addresses this? (i.e. not being able to style outside of a block). I am trying to add a div class to existing div that is coming from parent theme. I'm trying to do this in my subtheme (to avoid harming parent theme template files). The problem I have is that the div in question sits outside (wraps around) a block. so I am unsure of how to add classes to it as I will trigger errors if I write something in my subtheme twig (due to it extending parent theme's page.html.twig and therefore anything outside of a block is a no no)?