Handlebars: Change Partial Context

You can change partial context by providing more arguments after the partial name

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

Samples

The first argument will be new context for the partial

lightncandy

Used option: FLAG_RUNTIMEPARTIAL

Partials:
foo{{moo}}
Data:
array(
  "bar" => array(
    "moo" => "GOOD!"
  )
)
Template:
{{>foo bar}}
Result:
GOOD!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{>foo bar}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_RUNTIMEPARTIAL,
  "partials" => array(
    "foo" => "{{moo}}"
  )
));
$render = LightnCandy::prepare($php);
$data = array(
  "bar" => array(
    "moo" => "GOOD!"
  )
);
echo $render($data);
handlebars.js
Partials:
foo{{moo}}
Data:
{
  "bar": {
    "moo": "GOOD!"
  }
}
Template:
{{>foo bar}}
Result:
GOOD!
Source Code
var Handlebars = require('handlebars');
var template = '{{>foo bar}}';

var render = Handlebars.compile(template);
var data = {
  "bar": {
    "moo": "GOOD!"
  }
};
console.log(render(data, {
 partials: {
  "foo": "{{moo}}"
}}));

Named arguments will be merged into context

lightncandy

Used option: FLAG_RUNTIMEPARTIAL FLAG_NAMEDARG

Partials:
foo{{moo}}
Data:
array(
  "bar" => array(
    "moo" => "GOOD!"
  )
)
Template:
{{>foo bar moo='abc'}}
Result:
abc
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{>foo bar moo='abc'}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_NAMEDARG,
  "partials" => array(
    "foo" => "{{moo}}"
  )
));
$render = LightnCandy::prepare($php);
$data = array(
  "bar" => array(
    "moo" => "GOOD!"
  )
);
echo $render($data);
handlebars.js
Partials:
foo{{moo}}
Data:
{
  "bar": {
    "moo": "GOOD!"
  }
}
Template:
{{>foo bar moo='abc'}}
Result:
abc
Source Code
var Handlebars = require('handlebars');
var template = '{{>foo bar moo=\'abc\'}}';

var render = Handlebars.compile(template);
var data = {
  "bar": {
    "moo": "GOOD!"
  }
};
console.log(render(data, {
 partials: {
  "foo": "{{moo}}"
}}));

See Also...