LightnCandy option: helperresolver

Use helperresolver option to provide helper on demand when compile the template.

Name Resolver

helperresolver can return a function name for a helper.

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

$template = "{{foo}}, {{bar}}! {{moo}}";

function helper1 () {
    return 'Hello';
}

function helper2 () {
    return 'World';
}

$phpStr = LightnCandy::compile($template, array(
    'helperresolver' => function ($cx, $name) {
        switch ($name) {
        case 'foo':
            return 'helper1';
        case 'bar':
            return 'helper2';
        default:
            return;
        }
    }
));

$renderer = LightnCandy::prepare($phpStr);
echo $renderer();

Output

Hello, World! 

Runnable Resolver

helperresolver can return a Runnable function for a helper.

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

$template = "{{foo}}, {{bar}}! {{moo}}";

$phpStr = LightnCandy::compile($template, array(
    'helperresolver' => function ($cx, $name) {
        switch ($name) {
        case 'foo':
            return function () {
                return 'FOO';
            };
        case 'bar':
            return function () {
                return 'MOO';
            };
        default:
            return;
        }
    }
));

$renderer = LightnCandy::prepare($phpStr);
echo $renderer();

Output

FOO, MOO! 

Helper Lookup

helperresolver can return 1 or true when the $name is a helper. The compiled render function will not include the actural helper code, which can be provided at runtime.

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

$template = "{{foo}}, {{bar}}! {{moo}}";

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_EXTHELPER, // Do not provide helpers when compile
    'helperresolver' => function ($cx, $name) {
        return true;         // Every $name is valid helper name
    }
));

$renderer = LightnCandy::prepare($phpStr);

// Classes and instance for rendering
class BAR {
    private $secret = '';
    function __construct($start) {
        $this->secret = $start;
    }
    function getSecret() {
        return $this->secret;
    }
}

$bar = new BAR('haha');

class MOO {
    public static function test() {
        return 'MOO~';
    }
}

// You should provide helper functions when rendering
// because they are not in the $renderer
echo $renderer(0, array(
    'helpers' => array(
        'foo' => function () {        // The helper can be a callable
            return 'FOO!';
        },
        'bar' => [$bar, 'getSecret'], // The helper can be a method of an instance
        'moo' => 'MOO::test'          // The helper can be a static function of a class
    )
));

Output

FOO!, haha! MOO~

None Helper

When the helperresolver return null, the $name will not be resolved as a helper.

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

$template = "{{foo}}, {{bar}}! {{moo}}";

$phpStr = LightnCandy::compile($template, array(
    'helperresolver' => function ($cx, $name) {
        return;
    }
));

$renderer = LightnCandy::prepare($phpStr);

$data = array(
    'foo' => 'Hello',
    'bar' => 'World',
    'moo' => 'GOOD',
);

echo $renderer($data);

Output

Hello, World! GOOD

See Also...