Skip to content

Commit c5c4391

Browse files
committed
update registerFunction() docs to reflect breaking changes from v0.4.0
1 parent 4d88fdd commit c5c4391

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ _site
22
.sass-cache
33
composer.lock
44
pscss.phar
5+
/frameworks/
56
/src/
67
/vendor/

_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ exclude:
1414
- Gemfile.lock
1515
- Makefile
1616
- composer.lock
17+
- frameworks/
1718
- vendor/
1819

1920
# Github Pages

docs/index.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,29 +254,30 @@ data URI to reduce the number of requests on a page with a `data-uri` function.
254254
We 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

265266
The `$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

274275
For example, the value `10px` in PHP would be `array('number', 1, 'px')`. There
275276
is a large variety of types. Experiment with a debugging function like `print_r`
276277
to examine the possible inputs.
277278

278279
The 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
280281
automatically to the corresponding SCSS type.
281282

282283
As 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
299323
also don't do any type checking. This will have undefined results if we give it
300324
anything other than two numbers.
301325

0 commit comments

Comments
 (0)