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

查看:37
本文介绍了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...

推荐答案

如果要在所有捆绑包中共享公用资产,最好的选择是将它们放置在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天全站免登陆