【发布时间】:2012-06-05 12:07:46
【问题描述】:
我正在使用 Symfony2、Assetic 和 Twig。我有各种前端库——Backbone、jQuery、jQuery UI 和 Bootstrap。 Bootstrap 和 jQuery UI 都包含 CSS 和 JS 文件。
有没有一种方法可以定义它们需要包含的资源(包括依赖项),然后在 Twig / Assetic 中将所有这些资源包含在一个标签中?我希望拥有的是这样的:
// config.yml <!-- DOES NOT WORK -->
assetic:
resources:
jquery:
js: /filepath/to/jquery.js
jquery_ui:
dependencies: jquery
css: /filepath/to/jqueryui.css
js: /filepath/to/jqueryui.js
less:
js: /filepath/to/less.js
bootstrap:
dependencies: { less, jquery }
js: /filepath/to/bootstrap.js
css: /filepath/to/bootstrap.css
backbone:
dependencies: { jquery }
js: { /filepath/to/underscore.js, /filepath/to/backbone.js }
// view.html.twig
{% use jquery_ui %}
{% use bootstrap %}
// outputs all js and css for jQuery, jQueryUI, Less, Backbone, and Bootstrap
我发现了几个相关的问题:
- How to define Assetic resources in Symfony 2 yml or xml configuration file?
- Symfony2 Assetic + Twig Template JavaScript Inheritance
但似乎都不涉及在 config.yml 中定义资源。相反,他们在 base.html.twig 中定义它们,但这是我要避免的。
我尝试在 Twig 中使用use 标记,方法是定义一个名为“jquery_ui”的模板并在该块中使用{% stylesheets %} 和{% javascripts %},然后在base.html.twig 中放置{% use "jquery-ui.html" %}。但是,use 不会导入模板,因为它有正文。
【问题讨论】: