Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't override extended twig template block to empty

I'm using MopaBootstrapBundle in Symfony 2.1.3 with Twig templates. This bundle has base.html.twig template which contains scripts block:

{% block foot_script %}
    {# To only use a subset or add more js overwrite and copy paste this block
    To speed up page loads save a copy of jQuery in your project and override this block to include the correct path
    Otherwise the regeneration is done on every load in dev more with use_controller: true
     #}
    {% javascripts
        'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-modal.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-dropdown.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-scrollspy.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-tab.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-tooltip.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-popover.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-alert.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-button.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-collapse.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-carousel.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-typeahead.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-collection.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-subnav.js'
    %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock foot_script %}

I'm extending it in my template using:

{% extends 'MopaBootstrapBundle::base.html.twig' %}
{% block foot_script %}{% endblock foot_script %}

But it still tries to load Bundle's base.html.twig template and I get:

An exception has been thrown during the compilation of a template ("Unable to find file "@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js".") in "MopaBootstrapBundle::base.html.twig".


What I've found out is, that if you extend it like this:

{% extends 'MopaBootstrapBundle::base.html.twig' %}
{% block foot_script %}
    {% javascripts
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-typeahead.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-collection.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-subnav.js'
    %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock foot_script %}

Note the typeahead.js

I get:

An exception has been thrown during the compilation of a template ("Unable to find file "@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-typeahead.js".") in "MopaBootstrapBundle::base.html.twig".

If I remove just one line:

{% extends 'MopaBootstrapBundle::base.html.twig' %}
{% block foot_script %}
    {% javascripts
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-collection.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-subnav.js'
    %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock foot_script %}

I get:

An exception has been thrown during the compilation of a template ("Unable to find file "@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js".") in "MopaBootstrapBundle::base.html.twig".

It still tries to load all the scripst from base template.

Any suggestions how to override *foot_script* block to make it empty and not to load these JS files?

like image 642
Karmalakas Avatar asked Nov 12 '22 18:11

Karmalakas


1 Answers

What you want is to embed the MopaBootstrapBundle::base.html.twig instead of extending it. You should use Twig's embed tag:

{% embed 'MopaBootstrapBundle::base.html.twig' %}
    {% block foot_script %}{% endblock foot_script %}
{% endembed %}

From Twig's documentation:

The embed tag combines the behaviour of include and extends. It allows you to include another template's contents, just like include does. But it also allows you to override any block defined inside the included template, like when extending a template.

like image 195
Paulo Pinto Avatar answered Nov 15 '22 10:11

Paulo Pinto