如何动态保存所有.css路径,然后将它们传递给assetic进行压缩? (在 Symfony 2 中)
我在处理样式表和 JavaScript 方面遇到了困难。
assetic + twig 的所有示例都假设您事先知道所需文件的所有路径。
假设我想创建一个“关于”页面。
“about.html.twig”模板扩展自“layout.html.twig”。
布局必须有几个将在整个网站中使用的 .css 和 .js。
about 模板必须加载特定于它的其他 .css 和 .js 文件。
所以样式表块看起来像这样:
{% block stylesheets %}
{{ parent() }}
<link href='bla'> {# more stylesheets #}
{% endblock %}
那么在这种情况下我该如何使用 assetic 呢?
我是否应该从一开始就发送网站的所有资源,因为即使这样,性能也会得到提升?
问题是 .css 之间可能存在冲突,有时您加载 .css 只是为了覆盖您自己的基本设置,因此您不能只为整个站点创建单个 .css。
我还尝试设置一个数组来存储所有路径,然后循环,并将路径传递给 assetic。但 twig 似乎不支持语法 css[] = 'another_path.css' 。您必须在一次分配中设置整个数组
I'm having a hard time dealing with stylesheets and javascripts.
All the examples for assetic + twig assume that you know beforehand all the paths of the files you will need.
Suppose I want to create an 'About' page.
The 'about.html.twig' template extends from 'layout.html.twig'.
The layout must have a couple of .css and .js that will be used throughout the entire site.
The about template must load other .css and .js files that are specific to it.
So the stylesheets block looks something like this:
{% block stylesheets %}
{{ parent() }}
<link href='bla'> {# more stylesheets #}
{% endblock %}
Then how do I use assetic in this case?
Am I supposed to send all the assets of the site from the beginning, because even then, the performance will be boosted?
The problem is that there may be conflicts between the .css, sometimes you load a .css just to override your own base settings, so you can't just create a single .css for the whole site.
I also tried setting an array to store all the paths, then loop, and pass the paths to assetic. But it seems like the syntax css[] = 'another_path.css' is not supported by twig. You have to set the entire array in one assignment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您是正确的,资产 Twig 扩展的语法确实希望您确实知道要包含的所有资产。但这是设计使然,因为对于生产来说,您应该转储组合和缩小的 CSS/JS,以便 Web 服务器可以处理它,而无需涉及 PHP。
我对您的最佳建议是确定您愿意包含在网站所有页面上的所有样式表,并为它们进行资产样式表调用。
然后,对于需要其他样式表的特定页面,您可以在需要时进行第二次资产调用。
layout.html.twig
someview.html.twig
另一种解决方案(可能更简单)是重构 CSS,这样您就可以一次性下载所有样式。
Yes you are correct the syntax for the assetic Twig extensions does expect you do know all the assets you want to include. But that is by design because for production it is intended that you dump out the combined and minified CSS/JS so that it can handled by the web server without getting PHP involved.
My best suggestion for you is to identify all your stylesheets that you are happy to include on all pages of your site and make an assetic stylesheet call for them.
Then for specific pages where you need additional stylesheets you could make a second assetic call where required.
layout.html.twig
someview.html.twig
Another solution, which is possibly easier, is to just refactor your CSS so you can have all the styles downloaded in one go.