@@ -254,29 +254,30 @@ data URI to reduce the number of requests on a page with a `data-uri` function.
254254We can add and remove functions using the methods ` registerFunction ` and
255255` unregisterFunction ` .
256256
257- * ` registerFunction($functionName, $callable) ` assigns the callable value to
257+ * ` registerFunction($functionName, $callable, $prototype ) ` assigns the callable value to
258258 the name ` $functionName ` . The name is normalized using the rules of SCSS.
259259 Meaning underscores and dashes are interchangeable. If a function with the
260- same name already exists then it is replaced.
260+ same name already exists then it is replaced. The optional ` $prototype ` is an
261+ array of parameter names.
261262
262263* ` unregisterFunction($functionName) ` removes ` $functionName ` from the list of
263264 available functions.
264265
265266The ` $callable ` can be anything that PHP knows how to call using
266267` call_user_func ` . The function receives two arguments when invoked. The first
267- is an array of SCSS typed arguments that the function was sent. The second is a
268- reference to the current ` scss ` instance .
268+ is an array of SCSS typed arguments that the function was sent. The second is an
269+ array of SCSS values corresponding to keyword arguments (aka kwargs) .
269270
270- The * SCSS typed arguments* are actually just arrays that represent SCSS values.
271- SCSS has different types than PHP, and this is how ** scssphp** represents them
272- internally.
271+ The SCSS * typed arguments* and * kwargs * are actually just arrays that represent
272+ SCSS values. SCSS has different types than PHP, and this is how ** scssphp**
273+ represents them internally.
273274
274275For example, the value ` 10px ` in PHP would be ` array('number', 1, 'px') ` . There
275276is a large variety of types. Experiment with a debugging function like ` print_r `
276277to examine the possible inputs.
277278
278279The return value of the custom function can either be a SCSS type or a basic
279- PHP type. (such as a string or a number) If it's a PHP type, it will be converted
280+ PHP type (such as a string or a number). If it's a PHP type, it will be converted
280281automatically to the corresponding SCSS type.
281282
282283As an example, a function called ` add-two ` is registered, which adds two numbers
@@ -287,15 +288,38 @@ use Leafo\ScssPhp\Compiler;
287288
288289$scss = new Compiler();
289290
290- $scss->registerFunction('add-two', function($args) {
291- list($a, $b) = $args;
292- return $a[ 1] + $b[ 1] ;
293- });
291+ $scss->registerFunction(
292+ 'add-two',
293+ function($args) {
294+ list($a, $b) = $args;
295+
296+ return $a[1] + $b[1];
297+ }
298+ );
294299
295300$scss->compile('.ex1 { result: add-two(10, 10); }');
296301{% endhighlight %}
297302
298- It's worth noting that in this example we lose the units of the number, and we
303+ Using a prototype and kwargs, functions can take named parameters. In this next example,
304+ we register a function called ` divide ` which divides a named dividend by a named divisor.
305+
306+ {% highlight php startinline=true %}
307+ use Leafo\ScssPhp\Compiler;
308+
309+ $scss = new Compiler();
310+
311+ $scss->registerFunction(
312+ 'divide',
313+ function($args, $kwargs) {
314+ return $kwargs[ 'dividend'] [ 1 ] / $kwargs[ 'divisor'] [ 1 ] ;
315+ },
316+ array('dividend', 'divisor')
317+ );
318+
319+ $scss->compile('.ex2 { result: divide($divisor: 2, $dividend: 30); }');
320+ {% endhighlight %}
321+
322+ Note: in the above examples, we lose the units of the number, and we
299323also don't do any type checking. This will have undefined results if we give it
300324anything other than two numbers.
301325
0 commit comments