简体   繁体   English

在html.twig symfony2中导入js

[英]import js in html.twig symfony2

I try to import the JS file into my html.twig file. 我尝试将JS文件导入到我的html.twig文件中。 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. css工作得很好。 But the js file don't have any effect. 但是js文件没有任何效果。

asset() is not assetic. asset()不具有asset()性。 You don't have to speficy the asset in the javascripts macro. 您不必在javascripts宏中设置资产。 See how to use assetic and inlcude javascripts 了解如何使用资产和包含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. 请注意asset_url变量。

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 对于prod,你需要将它们转储到web路径下的物理文件中

php app/console assetic:dump --env=prod --no-debug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM