Use partialresolver
option to provide partial content on demand when compile the template.
The return value of partialresolver will be the content of the partial
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();
FOO, BAR! [partial not found]
You can implement any logic to load partial content from any storage
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();
[partial (file:foo.tmpl) not found], [partial (file:bar.tmpl) not found]! [partial (file:moo.tmpl) not found]
When partialresolver return null
it means `partial not found` and cause compile time error
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);
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