PHP code example of shyim / sasso

1. Go to this page and download the library: Download shyim/sasso library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shyim / sasso example snippets



use Sasso\Compiler;
use Sasso\CompileException;

$css = (new Compiler())
    ->setStyle(Compiler::STYLE_COMPRESSED)
    ->addImportPath(__DIR__ . '/scss')   // load path for @import / @use / @forward
    ->setUrl('app.scss')                 // enables byte-exact error snippets
    ->compile('@use "base"; .x { color: base.$brand; }');

try {
    (new Compiler())->setUrl('input.scss')->compile('.a { color: ');
} catch (CompileException $e) {
    // Error: unexpected end of input in value
    //   ╷
    // 1 │ .a { color:
    //   │            ^
    //   ╵
    //   input.scss 1:13  root stylesheet
    echo $e->getMessage();
}


use Sasso\Compiler;
use Sasso\Importer;

class ArrayImporter implements Importer {
    public function __construct(private array $files) {}

    // Given the unquoted URL as written (e.g. "base" for @import "base"),
    // return its SCSS/Sass source, or null if it cannot be found.
    public function resolve(string $url): ?string {
        return $this->files[$url] ?? null;
    }
}

$css = (new Compiler())
    ->setImporter(new ArrayImporter([
        'base'   => '$brand: #e91e63;',
        'mixins' => '@mixin big { font-size: 2rem; }',
    ]))
    ->compile('@use "base"; @import "mixins"; .btn { color: base.$brand; @

class DbImporter implements Importer {
    public function resolve(string $url): ?string {
        $row = $this->db->find($url);              // may throw DbException
        return $row?->source;                      // null => normal import error
    }
}

try {
    (new Compiler())->setImporter(new DbImporter($db))->compile($scss);
} catch (DbException $e) {
    // the original DbException, not a CompileException
}

echo (new Compiler())
    ->setSyntax(Compiler::SYNTAX_SASS)
    ->compile(".a\n  color: red");
ini
extension=sasso
console
$ cargo build --release
$ php -dextension=target/release/libsasso.dylib  your_script.php   # macOS
$ php -dextension=target/release/libsasso.so     your_script.php   # Linux