Handlebars: Built-in helper - lookup

Lookup value by provided base and key

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

Samples


lightncandy

Used option: FLAG_PARENT

Data:
array(
  "foo" => array(
    "Hello",
    "World"
  ),
  "bar" => array(
    "Hello" => "first",
    "World" => "second"
  )
)
Template:
{{#each foo}}
{{lookup ../bar .}}:{{.}}!
{{/each}}
Result:
first:Hello!
second:World!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}\n{{lookup ../bar .}}:{{.}}!\n{{/each}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_PARENT
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "Hello",
    "World"
  ),
  "bar" => array(
    "Hello" => "first",
    "World" => "second"
  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": [
    "Hello",
    "World"
  ],
  "bar": {
    "Hello": "first",
    "World": "second"
  }
}
Template:
{{#each foo}}
{{lookup ../bar .}}:{{.}}!
{{/each}}
Result:
first:Hello!
second:World!
Source Code
var Handlebars = require('handlebars');
var template = '{{#each foo}}\n{{lookup ../bar .}}:{{.}}!\n{{/each}}';

var render = Handlebars.compile(template);
var data = {
  "foo": [
    "Hello",
    "World"
  ],
  "bar": {
    "Hello": "first",
    "World": "second"
  }
};
console.log(render(data));

lightncandy

Used option: FLAG_PARENT FLAG_SPVARS

Data:
array(
  "foo" => array(
    "Hello" => "first",
    "World" => "second"
  ),
  "bar" => array(
    "first" => 1,
    "second" => 2
  )
)
Template:
{{#each foo}}{{@key}}:{{.}} => {{lookup ../bar .}}
{{/each}}
Result:
Hello:first => 1
World:second => 2
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{#each foo}}{{@key}}:{{.}} => {{lookup ../bar .}}\n{{/each}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_PARENT | LightnCandy::FLAG_SPVARS
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "Hello" => "first",
    "World" => "second"
  ),
  "bar" => array(
    "first" => 1,
    "second" => 2
  )
);
echo $render($data);
handlebars.js
Data:
{
  "foo": {
    "Hello": "first",
    "World": "second"
  },
  "bar": {
    "first": 1,
    "second": 2
  }
}
Template:
{{#each foo}}{{@key}}:{{.}} => {{lookup ../bar .}}
{{/each}}
Result:
Hello:first => 1
World:second => 2
Source Code
var Handlebars = require('handlebars');
var template = '{{#each foo}}{{@key}}:{{.}} => {{lookup ../bar .}}\n{{/each}}';

var render = Handlebars.compile(template);
var data = {
  "foo": {
    "Hello": "first",
    "World": "second"
  },
  "bar": {
    "first": 1,
    "second": 2
  }
};
console.log(render(data));