Skip to content

Instantly share code, notes, and snippets.

@timelsass
Created June 6, 2017 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelsass/52e3e768b01dd74aec3ef4ac8042500a to your computer and use it in GitHub Desktop.
Save timelsass/52e3e768b01dd74aec3ef4ac8042500a to your computer and use it in GitHub Desktop.
leafo compiler set var & import paths
<?php
// Include compiler if class isn't loaded.
if ( ! class_exists( '\Leafo\ScssPhp\Compiler' ) ) {
require get_theme_file_path( 'vendor/scssphp/scss.inc.php' );
}
// Create compiler instance.
$scss = new Compiler();
/**
* If your variables are in a _variables.scss file outside of the main
* scss file, you can set the import path to ensure the right path is used
*/
// Set @import path for ../../wp-content/themes/my-theme/scss/
$importPath = trailingslashit( get_theme_file_path( 'scss' ) );
$scss->setImportPaths( $importPath );
/**
* Add an array of variables you want to have compiled by leafo compiler.
* This example will look for the theme_mod to be set, and if it's not, it
* will use black as the default color.
*
* It will then create and add the scss variable with the passed value, or use
* the default color you specify, ie:
*
* $brand-primary: black;
*/
// Set variables to compile.
$mod = get_theme_mod( 'brandPrimary', 'black' ); // brand color theme_mod
$variables = [
'brand-primary' => $mod,
];
$scss->setVariables( $variables );
/**
* Run the compiler.
* $compiled will contain the compiled CSS that you can save to a file and have enqueued.
* If it's a customizer change for the user then you could hook into 'customize_save_after'
* to run this on save of new settings. Check $old !== $new to save overcompiling unchanged vars.
*/
// Run leafo compiler to compile SASS to CSS.
$content = 'body{ background-color: $brand-primary; }'
$compiled = $scss->compile( $content );
// var_dump( $compiled ); die; // => ( string ) 'body{ background-color: black; }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment