LightnCandy: Deep Dive Partials

Static Partial

LightnCandy will compile partials as included content by default for better performance.

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

$template = "\ncode for {{>foo}} just like\ncode for {{{bar}}}";

$phpStr = LightnCandy::compile($template, array(
    'partials' => array(
        'foo' => '{{{bar}}}'
    )
));

echo $phpStr;

Runtime Partial

With FLAG_RUNTIMEPARTIAL option, partials will be compiled as functions which can handle context change.

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

$template = "\ncode for {{>foo}} is different to\ncode for {{{bar}}}";

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_RUNTIMEPARTIAL,
    'partials' => array(
        'foo' => '{{{bar}}}'
    )
));

echo $phpStr;

Partial Usage

Only used partials will be compiled into render function by default.

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

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

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_RUNTIMEPARTIAL,
    'partials' => array(
        'foo' => '{{{foo}}}',
        'bar' => '{{{bar}}}',
        'moo' => '{{{moo}}} not used',
        'qoo' => '{{{qoo}}} not in render function',
    )
));

echo $phpStr;

Dynamic Partial

When you using dynamic partial in template, all provided partials will be compiled into render function.

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

$template = "{{>foo}}, {{>(bar)}}";

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_RUNTIMEPARTIAL,
    'helpers' => array(
        'bar' => function () {
            return 'bar';
        }
    ),
    'partials' => array(
        'foo' => '{{{foo}}}',
        'bar' => '{{{bar}}}',
        'moo' => '{{{moo}}} not used',
        'qoo' => '{{{qoo}}} in render function',
    )
));

echo $phpStr;

Compile Without Partials

With FLAG_ERROR_SKIPPARTIAL option it will not error when you compile without partials. This can reduce the size of render function.

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

$template = "{{>foo}}, {{>(bar)}}";

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_ERROR_SKIPPARTIAL,
    'helpers' => array(
        'bar' => function () {
            return 'bar';
        }
    )
));

// Warning: This code will cause 'partial not found' error
echo $phpStr;

Runtime Partials

You can provide more precompiled partials to render functions.

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

$template = "{{>foo}}, {{>(bar)}}";

$phpStr = LightnCandy::compile($template, array(
    'flags' => LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_ERROR_SKIPPARTIAL,
    'helpers' => array(
        'bar' => function () {
            return 'bar';
        }
    )
));
$renderer = LightnCandy::prepare($phpStr);

// It will be better to precompile partials and store them into files,
// prevent to use LightnCandy::prepare() in production environment.
$partials = array(
    'foo' => LightnCandy::prepare('return ' . LightnCandy::compilePartial('Hello')),
    'bar' => LightnCandy::prepare('return ' . LightnCandy::compilePartial('World')),
);

echo $renderer(null, array(
    'partials' => $partials
));

See Also...