You can use a subexpression as partial name. The partial name will be decided by the result of custom helper at run time.
This is a handlebars.js extension, mustache do not support this.
foo
will be partial nameUsed option: FLAG_ADVARNAME FLAG_RUNTIMEPARTIAL
Partials:bar1 | Hello |
---|---|
bar2 | World |
NULL
,
Hello, World
Check the code to know used helper codes
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{>(foo 1)}}, {{>(foo 2)}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_RUNTIMEPARTIAL,
"partials" => array(
"bar1" => "Hello",
"bar2" => "World"
),
"helpers" => array(
"foo" => function ($arg1) {
return 'bar' . $arg1;
}
)
));
$render = LightnCandy::prepare($php);
$data = NULL;
echo $render($data);
bar1 | Hello |
---|---|
bar2 | World |
null
,
Hello, World
Check the code to know used helper codes
var Handlebars = require('handlebars');
var template = '{{>(foo 1)}}, {{>(foo 2)}}';
Handlebars.registerHelper({
'foo': function (arg1) {
return 'bar' + arg1;
}
});
var render = Handlebars.compile(template);
var data = null;
console.log(render(data, {
partials: {
"bar1": "Hello",
"bar2": "World"
}}));
Used option: FLAG_ADVARNAME FLAG_RUNTIMEPARTIAL
Partials:bar1 | Hello |
---|---|
bar2 | World |
NULL
Can not find partial named as 'bar_not_found' !!
Check the code to know used helper codes
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{>(foo)}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_RUNTIMEPARTIAL,
"partials" => array(
"bar1" => "Hello",
"bar2" => "World"
),
"helpers" => array(
"foo" => function () {
return 'bar_not_found';
}
)
));
$render = LightnCandy::prepare($php);
$data = NULL;
echo $render($data);
bar1 | Hello |
---|---|
bar2 | World |
null
The partial bar_not_found could not be found
Check the code to know used helper codes
var Handlebars = require('handlebars');
var template = '{{>(foo)}}';
Handlebars.registerHelper({
'foo': function () {
return 'bar_not_found';
}
});
var render = Handlebars.compile(template);
var data = null;
console.log(render(data, {
partials: {
"bar1": "Hello",
"bar2": "World"
}}));
Used option: FLAG_ADVARNAME FLAG_RUNTIMEPARTIAL
Partials:en | Hello {{name}}. |
---|---|
es | Hola {{name}}. |
fr | Bonjour {{name}}. |
array(
"foo" => array(
array(
"template" => "en",
"name" => "alice"
),
array(
"template" => "es",
"name" => "bob"
),
array(
"template" => "fr",
"name" => "casey"
)
)
)
Hello alice. Hola bob. Bonjour casey.
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}{{> (lookup . 'template')}}{{/each}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_RUNTIMEPARTIAL,
"partials" => array(
"en" => "Hello {{name}}. ",
"es" => "Hola {{name}}. ",
"fr" => "Bonjour {{name}}."
)
));
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
array(
"template" => "en",
"name" => "alice"
),
array(
"template" => "es",
"name" => "bob"
),
array(
"template" => "fr",
"name" => "casey"
)
)
);
echo $render($data);
en | Hello {{name}}. |
---|---|
es | Hola {{name}}. |
fr | Bonjour {{name}}. |
{
"foo": [
{
"template": "en",
"name": "alice"
},
{
"template": "es",
"name": "bob"
},
{
"template": "fr",
"name": "casey"
}
]
}
Hello alice. Hola bob. Bonjour casey.
var Handlebars = require('handlebars');
var template = '{{#each foo}}{{> (lookup . \'template\')}}{{/each}}';
var render = Handlebars.compile(template);
var data = {
"foo": [
{
"template": "en",
"name": "alice"
},
{
"template": "es",
"name": "bob"
},
{
"template": "fr",
"name": "casey"
}
]
};
console.log(render(data, {
partials: {
"en": "Hello {{name}}. ",
"es": "Hola {{name}}. ",
"fr": "Bonjour {{name}}."
}}));
Used option: FLAG_ADVARNAME FLAG_RUNTIMEPARTIAL
Partials:a | Hello |
---|---|
b | World |
c | ! |
array(
"foo" => array(
"a",
"b",
"c"
)
)
HelloWorld!
Check the code to know used helper codes
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}{{> (bar .)}}{{/each}}";
$php = LightnCandy::compile($template, array(
"flags" => LightnCandy::FLAG_ADVARNAME | LightnCandy::FLAG_RUNTIMEPARTIAL,
"partials" => array(
"a" => "Hello",
"b" => "World",
"c" => "!"
),
"helpers" => array(
"bar" => function ($arg) {
return $arg;
}
)
));
$render = LightnCandy::prepare($php);
$data = array(
"foo" => array(
"a",
"b",
"c"
)
);
echo $render($data);
a | Hello |
---|---|
b | World |
c | ! |
{
"foo": [
"a",
"b",
"c"
]
}
HelloWorld!
Check the code to know used helper codes
var Handlebars = require('handlebars');
var template = '{{#each foo}}{{> (bar .)}}{{/each}}';
Handlebars.registerHelper({
'bar': function (arg) {
return arg;
}
});
var render = Handlebars.compile(template);
var data = {
"foo": [
"a",
"b",
"c"
]
};
console.log(render(data, {
partials: {
"a": "Hello",
"b": "World",
"c": "!"
}}));