Handlebars: Path

Handlebars extends mustache variable lookup with many path syntax

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

Samples

{{..}} means parent context.

lightncandy

Used option: FLAG_JSOBJECT FLAG_PARENT

Data:
array(
  "people" => array(
    "John",
    "Peter",
    "Mary"
  )
)
Template:
{{#people}}
{{..}}!
{{/people}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#people}}\n{{..}}!\n{{/people}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_JSOBJECT | LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
  "people" => array(
    "John",
    "Peter",
    "Mary"
  )
);
echo $render($data);
handlebars.js
Data:
{
  "people": [
    "John",
    "Peter",
    "Mary"
  ]
}
Template:
{{#people}}
{{..}}!
{{/people}}
Result:
[object Object]!
[object Object]!
[object Object]!
Source Code
var Handlebars = require('handlebars');
var template = '{{#people}}\n{{..}}!\n{{/people}}';

var render = Handlebars.compile(template);
var data = {
  "people": [
    "John",
    "Peter",
    "Mary"
  ]
};
console.log(render(data));

{{../foo}} will lookup parent context, then search for foo.

lightncandy

Used option: FLAG_PARENT

Data:
array(
  "foo" => array(
    "bar" => array(
      "moo" => "No"
    )
  ),
  "moo" => "Yes!"
)
Template:
{{#foo.bar}}
{{../moo}}
{{/foo.bar}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#foo.bar}}\n{{../moo}}\n{{/foo.bar}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "bar" => array(
      "moo" => "No"
    )
  ),
  "moo" => "Yes!"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": {
    "bar": {
      "moo": "No"
    }
  },
  "moo": "Yes!"
}
Template:
{{#foo.bar}}
{{../moo}}
{{/foo.bar}}
Result:
Yes!
Source Code
var Handlebars = require('handlebars');
var template = '{{#foo.bar}}\n{{../moo}}\n{{/foo.bar}}';

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

Everytime context changes, the new context will be pushed into the context stack. {{../../moo}} means using the item before previous item in the context stack as the base, then search for moo.

lightncandy

Used option: FLAG_JSOBJECT FLAG_PARENT

Data:
array(
  "foo" => array(
    "bar" => array(
      "moo" => array(
        "zoo" => "No"
      )
    )
  ),
  "moo" => "Yes!"
)
Template:
{{#foo.bar}}{{!<=first context push}}
 {{#moo}}{{!<=second context push}}
  {{zoo}} , {{../../moo}}
 {{/moo}}
 {{moo.zoo}} , {{../foo.bar.moo.zoo}}
{{/foo.bar}}
Result:
Command failed: php .exec_tmp_file 2>/dev/null
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#foo.bar}}{{!<=first context push}}\n {{#moo}}{{!<=second context push}}\n  {{zoo}} , {{../../moo}}\n {{/moo}}\n {{moo.zoo}} , {{../foo.bar.moo.zoo}}\n{{/foo.bar}}\n";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_JSOBJECT | LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "bar" => array(
      "moo" => array(
        "zoo" => "No"
      )
    )
  ),
  "moo" => "Yes!"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": {
    "bar": {
      "moo": {
        "zoo": "No"
      }
    }
  },
  "moo": "Yes!"
}
Template:
{{#foo.bar}}{{!<=first context push}}
 {{#moo}}{{!<=second context push}}
  {{zoo}} , {{../../moo}}
 {{/moo}}
 {{moo.zoo}} , {{../foo.bar.moo.zoo}}
{{/foo.bar}}
Result:

 
  No , Yes!
 No , No
Source Code
var Handlebars = require('handlebars');
var template = '{{#foo.bar}}{{!<=first context push}}\n {{#moo}}{{!<=second context push}}\n  {{zoo}} , {{../../moo}}\n {{/moo}}\n {{moo.zoo}} , {{../foo.bar.moo.zoo}}\n{{/foo.bar}}\n';

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

See Also...