Search code examples
csssymfonysassassetic

css and scss in twig file


I want to include a .css and .scss file in a twig template, is it necessary to write is this way?

{% stylesheets filter="sass"
    "css/proposal/edit.scss"
    "bundles/bmatznerfontawesome/scss/font-awesome.scss"
%}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

{% stylesheets
    "bundles/simplemde/debug/simplemde.css"
%}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

I dont like the redundant writing, but when I try to link the css file in the first block that is filtered for scss files it wont work.


Solution

  • Remove the filter attribute and add an 'apply_to' option in your config :

    // app/config.yml :
    
    assetic:
        # ...
        filters:
            sass:
                # ...
                apply_to: ".scss$"
    

    Then you can group everything in the first :

    {% stylesheets 
        "css/proposal/edit.scss"
        "bundles/bmatznerfontawesome/scss/font-awesome.scss"
        "bundles/simplemde/debug/simplemde.css"
    %}
        <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}