javascript - import js in html.twig symfony2 - Stack Overflow

admin2025-04-20  0

I try to import the JS file into my html.twig file. But it doesn't work. Could you give me some suggestions?

this is my layout file

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
{%block stylesheets %}

  <link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/bootstrap.css') }}" type="text/css" media="all" />
  <link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/layout.css') }}" type="text/css" media="all" />

  <link href="asset('bundles/ensjobologic/css/bootstrap-responsive.css" rel="stylesheet">

  <link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/flat-ui.css') }}" type="text/css" media="all" />

  {% endblock %}
  {% block javascripts %}
  {% endblock %}
</head>

this is the child file:

{% block javascripts %}
    {{ parent() }}
    {% javascripts 'js/jquery.tagsinput.js' %}
      <script src="{{ asset_url }}" type="text/javascript"></script>
    {% endjavascripts %}
{% endblock %}

the css work well. But the js file don't have any effect.

I try to import the JS file into my html.twig file. But it doesn't work. Could you give me some suggestions?

this is my layout file

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
{%block stylesheets %}

  <link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/bootstrap.css') }}" type="text/css" media="all" />
  <link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/layout.css') }}" type="text/css" media="all" />

  <link href="asset('bundles/ensjobologic/css/bootstrap-responsive.css" rel="stylesheet">

  <link rel="stylesheet" href="{{ asset('bundles/ensjobologic/css/flat-ui.css') }}" type="text/css" media="all" />

  {% endblock %}
  {% block javascripts %}
  {% endblock %}
</head>

this is the child file:

{% block javascripts %}
    {{ parent() }}
    {% javascripts 'js/jquery.tagsinput.js' %}
      <script src="{{ asset_url }}" type="text/javascript"></script>
    {% endjavascripts %}
{% endblock %}

the css work well. But the js file don't have any effect.

Share Improve this question edited Oct 19, 2015 at 11:10 Alaeddine 1,6972 gold badges26 silver badges48 bronze badges asked Jun 10, 2013 at 15:54 史京迪史京迪 4812 gold badges6 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

asset() is not assetic. You don't have to speficy the asset in the javascripts macro. See how to use assetic and inlcude javascripts

{% block javascripts %} 
    {{ parent() }}  
    {% javascripts '@EnsUserBundle/Resources/public/js/jquery.tagsinput.js'%}
         <script type="text/javascript" src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

Take notice at the asset_url variable.

EDIT: For dev the assets are delivered by a controller (in default settings), so every change is detected. Only after adding new files a cache clear is needed.

for prod you need to dump them to a physically file under the web path with

php app/console assetic:dump --env=prod --no-debug
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745115204a285819.html

最新回复(0)