5

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?

3
  • I think the problem is that first the {% javascripts %} tag is parsed and after that the {% block %} tags. I don't know how you can fix this. This issue by fabpot can maybe help you? Commented Nov 5, 2012 at 14:44
  • I even don't know if that's a solution (I don't think so), but if files are found in that bootstrap directory, then it works as expected. Commented Nov 6, 2012 at 7:33
  • As a quick fix, maybe load some dummy empty js file from your bundle like: {% extends 'MopaBootstrapBundle::base.html.twig' %} {% block foot_script %} {% javascripts '@MyBundle/Resources/public/js/dummy-file.js'%} {% endblock foot_script %} Commented Nov 6, 2012 at 14:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The embed tag was added in Twig 1.8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.