Twig - how to exclude other templates?

Viewed 943

I have got my base.html.twig that I want to use on most parts of my application.

It looks like this:

<html>
    <head>
        <title>{% block title %}Stack Example{% endblock %}</title>        

        <link rel="stylesheet" href="/resources_admin/dist/css/custom.css">

        {% block stylesheets %}{% endblock %}
    </head>
    <body class="hold-transition skin-blue sidebar-mini">
        {% include 'admin/top_menu/top_menu.html.twig' %}
        {% include 'admin/menu/menu.html.twig' %}
        {% block body %}{% endblock %}
        {% include 'admin/footer/footer.html.twig' %}

        <script src="/resources_admin/dist/js/custom.js"></script>

        {% block javascripts %}{% endblock %}
    </body>
</html>

Everything is fine. But I have one template where I want to use my base.html.twig but I don't want to include top_menu.html.twig.

Is right there any way to exclude something from template? Or I have to create new one?

1 Answers

You only have to set a block with a default value. Then overwrite it when you need to.

base.html.twig

<body class="hold-transition skin-blue sidebar-mini">
    {% block topmenu %}
        {# this is the default value #}
        {% include 'admin/top_menu/top_menu.html.twig' %}
    {% endblock %}
    [...]

In your specific template:

{# overwrite default top menu #}
{% block topmenu %}{% endblock %}

Thanks to Florent Destremau comment below, the shorter version is

{% block topmenu "" %}
Related