Created
June 6, 2017 02:51
-
-
Save timelsass/52e3e768b01dd74aec3ef4ac8042500a to your computer and use it in GitHub Desktop.
leafo compiler set var & import paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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