Handlebars: Advanced Variable

Some special charactors are not allowed to be in an expression. You can use [ and ] to surround an expression, then you can place special charactors inside it.

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

Samples

. means path

lightncandy

Used option: FLAG_ADVARNAME

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

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ADVARNAME
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "bar" => "path"
  ),
  "foo.bar" => "protected"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": {
    "bar": "path"
  },
  "foo.bar": "protected"
}
Template:
{{foo.bar}}
Result:
path
Source Code
var Handlebars = require('handlebars');
var template = '{{foo.bar}}';

var render = Handlebars.compile(template);
var data = {
  "foo": {
    "bar": "path"
  },
  "foo.bar": "protected"
};
console.log(render(data));

Now 'foo.bar' is protected by [ and ] and means to look up the key 'foo.bar' .

lightncandy

Used option: FLAG_ADVARNAME

Data:
array(
  "foo" => array(
    "bar" => "path"
  ),
  "foo.bar" => "protected"
)
Template:
{{[foo.bar]}}
Result:
protected
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{[foo.bar]}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ADVARNAME
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => array(
    "bar" => "path"
  ),
  "foo.bar" => "protected"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": {
    "bar": "path"
  },
  "foo.bar": "protected"
}
Template:
{{[foo.bar]}}
Result:
protected
Source Code
var Handlebars = require('handlebars');
var template = '{{[foo.bar]}}';

var render = Handlebars.compile(template);
var data = {
  "foo": {
    "bar": "path"
  },
  "foo.bar": "protected"
};
console.log(render(data));

Now '1 2' also feels good.

lightncandy

Used option: FLAG_ADVARNAME

Data:
array(
  "1 2" => "I feel good."
)
Template:
{{[1 2]}}
Result:
I feel good.
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{[1 2]}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ADVARNAME
));
$render = LightnCandy::prepare($php);
$data = array(
  "1 2" => "I feel good."
);
echo $render($data);
handlebars.js
Data:
{
  "1 2": "I feel good."
}
Template:
{{[1 2]}}
Result:
I feel good.
Source Code
var Handlebars = require('handlebars');
var template = '{{[1 2]}}';

var render = Handlebars.compile(template);
var data = {
  "1 2": "I feel good."
};
console.log(render(data));

lightncandy

Used option: FLAG_ADVARNAME

Data:
array(
  "1 2" => array(
    "#!@" => "Safe!"
  )
)
Template:
{{[1 2].[#!@]}}
Result:
Safe!
Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{[1 2].[#!@]}}";

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_ADVARNAME
));
$render = LightnCandy::prepare($php);
$data = array(
  "1 2" => array(
    "#!@" => "Safe!"
  )
);
echo $render($data);
handlebars.js
Data:
{
  "1 2": {
    "#!@": "Safe!"
  }
}
Template:
{{[1 2].[#!@]}}
Result:
Safe!
Source Code
var Handlebars = require('handlebars');
var template = '{{[1 2].[#!@]}}';

var render = Handlebars.compile(template);
var data = {
  "1 2": {
    "#!@": "Safe!"
  }
};
console.log(render(data));

See Also...