11

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.

3
  • 4
    The stylesheets tag is no related to Twig. This tag is contributed by AsseticBundle. Could you clarify your question, I have hard time understanting what you try to achieve :)
    – Matt
    Jun 14, 2012 at 13:41
  • @Matt Well I want to keep the benefits that Assetic gives me, while running the stylesheet through Twig first. Jun 18, 2012 at 6:38
  • @Xeross if you could say more specifically why you want to do this, you may see better results. Aug 30, 2013 at 18:18

2 Answers 2

3

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>
1
  • 1
    Yes this did come to mind, however you lose the benefit of having your styles in a separately cached file. Jun 18, 2012 at 6:39
1

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.