LightnCandy option: partialresolver

Use partialresolver option to provide partial content on demand when compile the template.

Partial Resolver

The return value of partialresolver will be the content of the partial

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

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

$phpStr = LightnCandy::compile($template, array(
    'partialresolver' => function ($cx, $name) {
        switch ($name) {
        case 'foo':
            return 'FOO';
        case 'bar':
            return 'BAR';
        default:
            return '[partial not found]';
        }
    }
));

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

Output

FOO, BAR! [partial not found]

File Resolver

You can implement any logic to load partial content from any storage

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

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

$phpStr = LightnCandy::compile($template, array(
    'partialresolver' => function ($cx, $name) {
        if (file_exists("$name.tmpl")) {
            return file_get_contents("$name.tmpl");
        }
        return "[partial (file:$name.tmpl) not found]";
    }
));

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

Output

[partial (file:foo.tmpl) not found], [partial (file:bar.tmpl) not found]! [partial (file:moo.tmpl) not found]

Partial Not Found

When partialresolver return null it means `partial not found` and cause compile time error

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

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

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

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

Output

Can not find partial for 'foo', you should provide partials or partialresolver in options
Can not find partial for 'bar', you should provide partials or partialresolver in options
Can not find partial for 'moo', you should provide partials or partialresolver in options

See Also...