Run CSS file through Twig when using {% stylesheets %} tag in Twig with Symfony2

半腔热情 提交于 2020-01-12 07:13:06

问题


I'm including CSS stylesheets in my template like so:

{% stylesheets
    "@SomeBundle/Resources/assets/css/default.css.twig"
    "@SomeBundle/Resources/assets/css/global.css.twig"
%}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

However I want to run these CSS files through Twig, is this in any way possible while using the {% stylesheets %} tag or does this require some other approach. I've already tried enabling a twig filter but that does not exist.


回答1:


You could do it if you load the css as an internal stylesheet. Something like this:

{% block stylesheets %}
    {{ parent() }}
    {% include 'AcmeBundle:Bundle:mycss.css.twig' %}
{% endblock %}

And then the mycss.css.twig template would contain:

<style type="text/css">
    /* */
</style>



回答2:


Most common things you'd want to do by processing CSS with Twig should be possible with Sass, LESS or similar, which can be applied as Assetic filters. First tutorial Google threw up was this one: http://alexandre-salome.fr/blog/Sass-Compass-Assetic-In-Ten-Minutes

Admittedly, that doesn't look like much help if you're unable to run Ruby/Node.js/whatever on the production server you're developing for, but it should be possible to create an Assetic filter based on one of the PHP ports of Sass/LESS (assuming they're any good) if that's the case.



来源:https://stackoverflow.com/questions/11033172/run-css-file-through-twig-when-using-stylesheets-tag-in-twig-with-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!