在Symfony2中使用assetic加载一个js文件 [英] Load one js file using assetic inside Symfony2

查看:24
本文介绍了在Symfony2中使用assetic加载一个js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Symfony 2 的新手.以前我经常使用 Codeigniter.现在我正在探索资产,我无法理解如何将单个文件添加到已经加载的 JS 文件堆栈中.

I am new to Symfony 2. Previously I worked a lot with Codeigniter. Now that I am exploring assetic, I am not able to understand how I can add a single file to the stack of JS files which are already being loaded.

我有一个主页 twig 文件,内容如下:

I have a main page twig file which has something like this:

{% javascripts '@BlogBlogBundle/Resources/public/js/vendor/*' output='js/combined.js' %}
  <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

现在,如果所有 JS 文件都在 vendor 文件夹中,这可以正常工作.但是,如果我在名为 contact 的文件夹中有一个 JS 文件 contact.js 并且我希望它仅在联系人页面上可见,该怎么办.所以,当我在联系页面树枝文件的块体中添加这样的代码时

Now this works fine if all the JS files are inside the vendor folder. But what if I have a JS file contact.js inside a folder called contact and I want that to be visible only on contact page. So, when I added such a code inside block body of contact page twig file

{% javascripts '@BlogBlogBundle/Resources/public/js/home/*' output='js/home_combined.js' %}
  <script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

contact.js 在正文结束之前加载的所有其他 js 文件之前.

the contact.js is coming before all other js files which are being loaded before the body ends.

在 codeigniter 中,使用 Carabiner 库,我可以设置我想在所有页面上加载的默认文件,例如 jQuery.如果页面有任何特定文件,我可以在该特定控制器中执行此操作.

In codeigniter, using the Carabiner library I could set up the default files which I want to load on all pages like jQuery. And if there are any specific file for a page, I can do that inside that particular controller.

请让我知道如何在 Symfony 中也能做到这一点.

Please let me know how I can do this inside Symfony also.

推荐答案

您可以像这样将文件添加到您的资产中:

You can add a file to your assets like this:

{% javascripts
    '@YourBundle/Resources/public/js/*'
    '@AnotherBundle/Resources/public/js/some.js'
    '@YourBundle/Resources/public/vendor/someother.js'
    output="filename.js"
%}
    <script src="{{ asset_url }}"></script>
{% endjavascripts %}

为了拥有预定义的资产集合,然后您可以将其用作例如@js_default",您可以按照 我的答案在这里.

In order to have pre-defined asset collections which you can then use as for example '@js_default' you can add these in your assetic.assets configuration as suggested in my answer here.

这篇关于在Symfony2中使用assetic加载一个js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆