A Twig extension/function to render inline SASS files for SEO purposes or to render CSS files with easy customizability

1.0.5 2016-11-16 14:12 UTC

This package is not auto-updated.

Last update: 2022-08-06 07:56:03 UTC


A Twig extension/function to render inline SASS files for SEO purposes. You can also use twig variables inside your sass files.


Add RenderFunction to Twig:

$twig = new Twig_Environment(new Twig_Loader_Filesystem());
$renderFunction = new \Richard87\TwigSass\RenderFunction(
    new \Leafo\ScssPhp\Compiler(),
    new Richard87\NullCache\NullCachePool()

Or if you are using Symfony, add this to services.yml:

    class: Leafo\ScssPhp\Compiler

    class: Richard87\TwigSass\RenderFunction
    arguments: ['@app.sass_compiler','']
      - { name: twig.extension}


RenderFunction takes 4 arguments:

  • Leafo\ScssPhp\Compiler: does the actual compilation of your sass file
  • PSR6 Cache Pool: A PSR6 enabled Cache pool is requried for any useful performance
  • importDir: The location SCSS should look for libraries, usually where your node_modules are (mine is in web)
  • sassFormater: How your output should look like. Takes a classname in string format. Can be any of these values:
    • "Leafo\ScssPhp\Formatter\Expanded"
    • "Leafo\ScssPhp\Formatter\Nested"
    • "Leafo\ScssPhp\Formatter\Compressed"
    • "Leafo\ScssPhp\Formatter\Compact"
    • "Leafo\ScssPhp\Formatter\Crunched"


Then render inline styles like this:

    {{ renderSass("styles.scss.twig") }}


$brand-primary: #59cc4a;
$border-radius: 0;
$border-radius-lg: 0;
$border-radius-sm: 0;
@import "node_modules/bootstrap/scss/bootstrap";

Notice that node_modules is inside the web directory, but that can be easily changed with the importRootDir variable.

You can also use the included renderSass filter like this:

    {{ '$brand-primary: #59cc4a;@import "node_modules/bootstrap/scss/bootstrap";'|renderSass }}


  • Create a Symfony hook that automatically renders *.scss.twig templates
  • Write tests