sensational / sassphp

PHP bindings to libsass - fast, native Sass parsing in PHP!
Other
230 stars 40 forks source link

Add support for custom importer and functions #40

Closed pilif closed 5 years ago

pilif commented 5 years ago

This PR will adds support for register custom functions and importers written in PHP to be available during the compilation process.

Importers:

$sass->setImporter(function($in){
    var_dump($in);
    return ($in == 'flupp')
        ? [ [null, '.compiled2 { color: yellow !important }' ],
            [__DIR__.'/otherfile.scss'],
        ]
        : null;
});

In your callback, return either an array or an array of arrays. If you set the 0th element, the compiler will instead process the file provided there.

If you set the 1st element, the compiler will use whatever you return there in place of the import.

Return null if you want the default processing to happen.

And functions

$sass = new Sass();
$sass->setFunctions([
    'a($a)' => function($in, $path_info){
        return "hello $in";
    },
    'b($a)' => function($in, $path_info){
        return "goodbye $in";
    },
]);

echo $sass->compile('body { content: a("foo"); } h1 { content: b("bar"); }');

At this point, the functions API is still very limited and extremely stringy, converting all arguments to a single string and only allowing you to return a string.

But hey - it’s a start.