The project can be loaded through a
composer generated auto-loader.
Alternatively, the entire project can be loaded through a utility file. Just include it somewhere to start using it:
In order to manually compile code from PHP you must create an instance of the
Compiler class. The typical flow is to create the instance, set any compile time
options, then run the compiler with the
compile($scssCode)will attempt to compile a string of SCSS code. If it succeeds then the CSS will be returned as a string. If there is any error, an exception is thrown with an appropriate error message.
When you import a file using the
@import directive, the current path of your
PHP script is used as the search path by default. This is often not what
you want, so there are two methods for manipulating the import path:
addImportPath($path) will append
$path to the list of the import
paths that are searched.
setImportPaths($pathArray) will replace the entire import path with
$pathArray. The value of
$pathArray will be converted to an array if it
isn’t one already.
If the import path is set to
array() then importing is effectively disabled.
The default import path is
array(''), which means the current directory.
Besides adding static import paths, it’s also possible to add custom import functions. This allows you to load paths from a database, or HTTP, or using files that SCSS would otherwise not process (such as vanilla CSS imports).
A list of the compiled files (both the primary file and its imports) can be
retrieved using the
getParsedFiles()returns an associative array where the keys are the file names and the values are the corresponding file’s last-modified timestamp.
You can preset variables before compilation by using the
method. If the variable is also defined in your scss source, use the
flag to prevent your preset variables from being overridden.
Note: the value is the scss source to be parsed. If you want to parse a string,
you have to represent it as a string, e.g,,
'var' => '"string"'.
Likewise, you can retrieve the preset variables using the
method, and unset a variable using the
It’s possible to customize the formatting of the output CSS by changing the default formatter.
Five formatters are included:
We can change the formatting using the
setFormatter($formatterName)sets the current formatter to
$formatterName, the name of a class as a string that implements the formatting interface. See the source for
ScssPhp\ScssPhp\Formatter\Expandedfor an example.
Given the following SCSS:
The formatters output the following:
You can also change the number precision using the
method. (The default precision is 5.)
You can output the original SCSS line numbers within the compiled CSS file for better frontend debugging.
This works well in combination with frontend debugging tools such as https://addons.mozilla.org/de/firefox/addon/firecompass-for-firebug/
To activate this feature, call the
setLineNumberStyle method after creating a new instance of class
Source Maps are useful in debugging compiled css files using browser developer tools.
To enable source maps, use the
setSourceMap()' and setSourceMapOptions()` methods.
It’s possible to register custom functions written in PHP that can be called
from SCSS. Some possible applications include appending your assets directory
to a URL with an
asset-url function, or converting image URLs to an embedded
data URI to reduce the number of requests on a page with a
We can add and remove functions using the methods
registerFunction($functionName, $callable, $prototype) assigns the callable value to
$functionName. The name is normalized using the rules of SCSS.
Meaning underscores and dashes are interchangeable. If a function with the
same name already exists then it is replaced. The optional
$prototype is an
array of parameter names.
$functionName from the list of
$callable can be anything that PHP knows how to call using
call_user_func. The function receives two arguments when invoked. The first
is an array of SCSS typed arguments that the function was sent. The second is an
array of SCSS values corresponding to keyword arguments (aka kwargs).
The SCSS typed arguments and kwargs are actually just arrays that represent SCSS values. SCSS has different types than PHP, and this is how scssphp represents them internally.
For example, the value
10px in PHP would be
array('number', 1, 'px'). There
is a large variety of types. Experiment with a debugging function like
to examine the possible inputs.
The return value of the custom function can either be a SCSS type or a basic PHP type (such as a string or a number). If it’s a PHP type, it will be converted automatically to the corresponding SCSS type.
As an example, a function called
add-two is registered, which adds two numbers
together. PHP’s anonymous function syntax is used to define the function.
Using a prototype and kwargs, functions can take named parameters. In this next example,
we register a function called
divide which divides a named dividend by a named divisor.
Note: in the above examples, we lose the units of the number, and we also don’t do any type checking. This will have undefined results if we give it anything other than two numbers.
For feature detection via the
feature-exists() built-in function, you can register
your custom feature using the
If your web appilcation compiles SCSS on-the-fly, you need to handle any potential exceptions thrown by the Compiler. This is especially important in a production environment where the content may be untrusted (e.g., user uploaded) because the exception stack trace may contain sensitive data.
If your web application allows for arbitrary
@import paths, you should
open_basedir setting at run-time to mitigate vulnerability to
local file inclusion (LFI) attack.
Server class is described here: /scssphp/docs/server.html.