Handlebars: Partial Block

By default it will cause error when a template try to render a missing partial. You can provide failover partial by partial block, it will only rendered when the partial is not provided

This is a handlebars.js extension, mustache do not support this.

Samples

Provide failover partial

lightncandy

Used option: FLAG_RUNTIMEPARTIAL

Data:
NULL
Template:
{{#> testPartial}}
  ERROR: testPartial is not found!
{{/testPartial}}
Result:
  ERROR: testPartial is not found!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#> testPartial}}\n  ERROR: testPartial is not found!\n{{/testPartial}}\n";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_RUNTIMEPARTIAL
));
$render = LightnCandy::prepare($php);
$data = NULL;
echo $render($data);
handlebars.js
Data:
Template:
{{#> testPartial}}
  ERROR: testPartial is not found!
{{/testPartial}}
Result:
  ERROR: testPartial is not found!
Source Code
var Handlebars = require('handlebars');
var template = '{{#> testPartial}}\n  ERROR: testPartial is not found!\n{{/testPartial}}\n';

var render = Handlebars.compile(template);
var data = undefined;
console.log(render(data));

When partial provided, partial block will be ignored

lightncandy

Used option: FLAG_RUNTIMEPARTIAL

Partials:
testPartialYes I am partial.
Data:
NULL
Template:
{{#> testPartial}}
  ERROR: testPartial is not found!
{{/testPartial}}
Result:
Yes I am partial.
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#> testPartial}}\n  ERROR: testPartial is not found!\n{{/testPartial}}\n";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_RUNTIMEPARTIAL,
  "partials" => array(
    "testPartial" => "Yes I am partial."
  )
));
$render = LightnCandy::prepare($php);
$data = NULL;
echo $render($data);
handlebars.js
Partials:
testPartialYes I am partial.
Data:
Template:
{{#> testPartial}}
  ERROR: testPartial is not found!
{{/testPartial}}
Result:
Yes I am partial.
Source Code
var Handlebars = require('handlebars');
var template = '{{#> testPartial}}\n  ERROR: testPartial is not found!\n{{/testPartial}}\n';

var render = Handlebars.compile(template);
var data = undefined;
console.log(render(data, {
 partials: {
  "testPartial": "Yes I am partial."
}}));

use {{> @partial-block}} to render partial block

lightncandy

Used option: FLAG_RUNTIMEPARTIAL FLAG_SPVARS

Partials:
testPartialYes I am partial. If I am not here, '{{> @partial-block}}' will replace me.
Data:
NULL
Template:
{{#> testPartial}}
  ERROR: testPartial is not found!
{{/testPartial}}
Result:
Yes I am partial. If I am not here, '  ERROR: testPartial is not found!
' will replace me.
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#> testPartial}}\n  ERROR: testPartial is not found!\n{{/testPartial}}\n";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_SPVARS,
  "partials" => array(
    "testPartial" => "Yes I am partial. If I am not here, '{{> @partial-block}}' will replace me."
  )
));
$render = LightnCandy::prepare($php);
$data = NULL;
echo $render($data);
handlebars.js
Partials:
testPartialYes I am partial. If I am not here, '{{> @partial-block}}' will replace me.
Data:
Template:
{{#> testPartial}}
  ERROR: testPartial is not found!
{{/testPartial}}
Result:
Yes I am partial. If I am not here, '  ERROR: testPartial is not found!
' will replace me.
Source Code
var Handlebars = require('handlebars');
var template = '{{#> testPartial}}\n  ERROR: testPartial is not found!\n{{/testPartial}}\n';

var render = Handlebars.compile(template);
var data = undefined;
console.log(render(data, {
 partials: {
  "testPartial": "Yes I am partial. If I am not here, '{{> @partial-block}}' will replace me."
}}));

See Also...