JavaScript在Symfony中加载两次

时间:2014-01-31 05:51:17

标签: javascript symfony

当我扩展我的base.html.twig时,我的JS正在加载两次。这是我的代码:

{% block javascripts %}
{% javascripts
'@MyBundle/Resources/public/app/src/lib/jquery.js'
'@MyBundle/Resources/public/app/src/lib/jquery-ui.js'
'@MyBundle/Resources/public/app/src/lib/angular.js'
'@MyBundle/Resources/public/app/src/lib/ui-bootstrap-tpls-0.10.0.js'
'@MyBundle/Resources/public/app/src/lib/fullcalendar.js'
'@MyBundle/Resources/public/app/calendar.js'
'@MyBundle/Resources/public/app/src/lib/angular-route.js'
'@MyBundle/Resources/public/app/schedulePlanner.js'
 %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

1 个答案:

答案 0 :(得分:14)

我遇到了一个类似的问题,原因是错误的{% endblock %}标记造成的。

这是我的基本观点:

{# app/Resources/views/base.html.twig #}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}{% endblock %}</title>
        {% block header %}{% endblock %}
        {% block stylesheets %}{% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        <!--[if lt IE 9]>
            <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a>.</p>
        <![endif]-->

        {# debug #}
        {% if pre is defined and pre is not empty and app.environment == 'dev' %}
            <pre>
                {{ pre }}
            </pre>
        {% endif %}

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

这是扩展的包模板:

{% extends "::base.html.twig" %}

{# title #}
{% block title %}{% endblock %}

{% block header %}
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <meta name="viewport" content="width=device-width">
    <link href='http://fonts.googleapis.com/css?family=Pathway+Gothic+One' rel='stylesheet' type='text/css'>
{% endblock %}

{# style #}
{% block stylesheets %}
{% endblock %}

{# body #}
{% block body %}

    {% block content_header %}
        <div class="header"><div class="header-in">
            <header>
            </header>
        </div></div>

        <div class="nav"><div class="nav-in">
            <nav>
                <ul id="menu" class="menu clearfix">
                    {% block content_header_nav %}
                    {% endblock %}
                </ul>
            </nav>
        </div></div>
    {% endblock %}

    <div class="block"><div class="block-in">

        <div class="content"><div class="content-in clearfix">

            {% set notices = app.session.flashbag.get('notices') %}
            {% if notices is not empty %}
            <ul class="msg ajax-flash-msg">
              {% for notice in notices %}
              <li>{{ notice }}</li>
              {% endfor %}
            </ul>
            {% endif %}

            {% block content %}{% endblock %}

        </div></div>

    </div></div>

    <div class="footer"><div class="footer-in">
        <footer>
            {% block footer %}{% endblock %}
        </footer>
    </div></div>

    {# javascript #}
    {% block javascripts %}
    <script src="{{ asset('assets/require.js') }}"></script>
    {% endblock %}

{% endblock %}

你会注意到我在包模板中的body块里面有我的javascript块,导致它在html输出到浏览器时呈现两次。

要修复我将javascript块移到了body块之外。