Symfony2:如何在包之间共享 js 库和 css [英] Symfony2: How to share js libs and css between bundles

查看:33
本文介绍了Symfony2:如何在包之间共享 js 库和 css的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的捆绑包:MainBundle(主页)、SecurityBundle(登录、注册)、MessageBundle(消息系统)、ShopBundle.

I have different Bundles: MainBundle (Homepage), SecurityBundle (Login, Registration), MessageBundle (Message System), ShopBundle.

我也遵循三阶段布局模式(::base.html.twig、AcmeMainBundle::layout.html.twig、AcmeMainBundle:Default:index.html.twig).

I also follow the three stage layout schema (::base.html.twig, AcmeMainBundle::layout.html.twig, AcmeMainBundle:Default:index.html.twig).

但是我在通过应用程序(例如 jquery)共享通用 js 库和定义 base.css(设置一些基类、背景、字体等)时遇到问题

But I have problems sharing common js libaries through the application (e.g. jquery) and defining a base.css (which sets some base classes, backgrounds, fonts etc.)

那么使用共享 css 和 js 而又不必失去资产支持的最佳方法是什么?

So whats the best approach to use shared css and js withouth having to lose assetic support?

一个想法是创建一个 CommonBundle 来保存所有全局 js 和 css 以及一些布局文件,但我认为这不是处理这个问题的最佳方法......

One idea would be to create a CommonBundle which holds all global js and css and some layout files but I don't think this is the best way to handle this...

推荐答案

如果您想在所有 bundle 之间共享公共资源,最好的选择是将它们放在 app/Resources/public目录.例如:

If you want to share common assets among all your bundles, the best choice is to place them in the app/Resources/public directory. For example:

app/Resources/Public
|-- css
|   `-- base.css
|-- js
|   `-- jquery.js

然后你可以在你的布局中引用它们,如下所示:

Then you can reference them in your layout as follow:

{% block stylesheets %}
  {% stylesheets '../app/Resources/public/css/*' %}
    <link rel="stylesheet" type="text/css" charset="UTF-8" media="all" href="{{ asset_url }}"/>
  {% endstylesheets %}
{% endblock %}

{% block javascripts %}
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  {% javascripts '../app/Resources/public/js/*' %}
    <script type="text/javascript" src="{{ asset_url }}"></script>
  {% endjavascripts %}
{% endblock %}

备注:如您所见,对于像 jQuery 这样的常用库,最好的选择仍然是使用 Google 托管的常用缓存版本.这种做法可以加快您的应用程序响应时间.

Remark: As you can see, for common libraries like jQuery, the best choice remains the use of common cached version hosted at Google. This kind of practice can speedup your application response time.

这篇关于Symfony2:如何在包之间共享 js 库和 css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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