LightnCandy: The $options Object

The $options object will be appended into custom helper arguments, you can receive many usefull infomation from this object.

Named Arguments

You can pass arguments to your custom helpers as key=value pairs, these named arguments can be received by the $options object as key value pairs in $options['hash'].

Source Code
require_once('./vendor/autoload.php');
use LightnCandy\LightnCandy;

function my_helper ($arg1, $options) {
    return "ARG1: $arg1\nNamed arguments: " . print_r($options['hash'], true);
}

// foo will be $arg1
// named arguments will be in $options['hash'] as key => value pairs
$template = '{{{my_helper foo bar="abc" [na me]="string!" test=foo}}}';

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_HANDLEBARS,
    'helpers' => array(
        'my_helper',
    )
));
$renderer = LightnCandy::prepare($phpStr);
echo $renderer(array(
    'foo' => 'Hello!'
));

Inner Block

You can render the content of inner block in your block custom helper, it can be done by running $options['fn']().

Source Code
require_once('./vendor/autoload.php');
use LightnCandy\LightnCandy;

function my_helper ($options) {
    return "#Inner Content: '" . $options['fn']() . "'";
}

$template = '{{#my_helper}}Hello World!{{/my_helper}}';

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_HANDLEBARS,
    'helpers' => array(
        'my_helper',
    )
));
$renderer = LightnCandy::prepare($phpStr);
echo $renderer();

Change Context

If you pass parameter into the function $options['fn'], it will become new context of the inner block.

Source Code
require_once('./vendor/autoload.php');
use LightnCandy\LightnCandy;

function my_helper ($options) {
    return "#Inner Content: '" . $options['fn']("This is new context") . "'";
}

$template = '{{#my_helper}}Hello, {{.}}!{{/my_helper}}';

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_HANDLEBARS,
    'helpers' => array(
        'my_helper',
    )
));
$renderer = LightnCandy::prepare($phpStr);
echo $renderer();

See Also...