Handlebars: Custom Helper

You can provide custom helper to handle logic

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

Samples


lightncandy
Data:
array(
  "foo" => 1,
  "bar" => "1"
)
Template:
{{isequal foo bar}}
Result:
No

Check the code to know used helper codes

Source Code
require('./vendor/autoload.php');
use LightnCandy\LightnCandy;
$template = "{{isequal foo bar}}";

$php = LightnCandy::compile($template, array(
  "helpers" => array(
    "isequal" => function ($arg1, $arg2) {
      return ($arg1 === $arg2) ? 'Yes' : 'No';
    }
  )
));
$render = LightnCandy::prepare($php);
$data = array(
  "foo" => 1,
  "bar" => "1"
);
echo $render($data);
handlebars.js
Data:
{
  "foo": 1,
  "bar": "1"
}
Template:
{{isequal foo bar}}
Result:
No

Check the code to know used helper codes

Source Code
var Handlebars = require('handlebars');
var template = '{{isequal foo bar}}';

Handlebars.registerHelper({
  'isequal': function (arg1, arg2) {
    return (arg1 === arg2) ? 'Yes' : 'No';
  }
});
var render = Handlebars.compile(template);
var data = {
  "foo": 1,
  "bar": "1"
};
console.log(render(data));

See Also...