The $options
object will be appended into custom helper arguments, you can receive many usefull infomation from this object.
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']
.
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!'
));
ARG1: Hello!
Named arguments: Array
(
[bar] => abc
[na me] => string!
[test] => Hello!
)
You can render the content of inner block in your block custom helper, it can be done by running $options['fn']()
.
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();
#Inner Content: 'Hello World!'
If you pass parameter into the function $options['fn']
, it will become new context of the inner block.
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();
#Inner Content: 'Hello, This is new context!'