Handlebars: Named Arguments

You can pass named arguments into custom helper or partial.

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

Samples


lightncandy

Used option: FLAG_NAMEDARG

Data:
array(
  "bar" => "ABC",
  "foo" => "DEF"
)
Template:
{{helper bar foo=123}}
Result:
ABC,123

Check the code to know used helper codes

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

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_NAMEDARG,
  "helpers" => array(
    "helper" => function ($arg0, $options) {
      return $arg0 . ',' . $options['hash']['foo'];
    }
  )
));
$render = LightnCandy::prepare($php);
$data = array(
  "bar" => "ABC",
  "foo" => "DEF"
);
echo $render($data);
handlebars.js
Data:
{
  "bar": "ABC",
  "foo": "DEF"
}
Template:
{{helper bar foo=123}}
Result:
ABC,123

Check the code to know used helper codes

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

Handlebars.registerHelper({
  'helper': function (arg0, options) {
    return arg0 + ',' + options['hash']['foo'];
  }
});
var render = Handlebars.compile(template);
var data = {
  "bar": "ABC",
  "foo": "DEF"
};
console.log(render(data));

lightncandy

Used option: FLAG_NAMEDARG

Data:
array(
  "bar" => "Yahoo!",
  "foo" => "http://yahoo.com/"
)
Template:
{{{link href=foo title=bar}}}
Result:
<a href="http://yahoo.com/">Yahoo!</a>

Check the code to know used helper codes

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

$php = LightnCandy::compile($template, array(
  "flags" => LightnCandy::FLAG_NAMEDARG,
  "helpers" => array(
    "link" => function ($options) {
      return '<a href="' . $options['hash']['href'] . '">' . $options['hash']['title'] . '</a>';
    }
  )
));
$render = LightnCandy::prepare($php);
$data = array(
  "bar" => "Yahoo!",
  "foo" => "http://yahoo.com/"
);
echo $render($data);
handlebars.js
Data:
{
  "bar": "Yahoo!",
  "foo": "http://yahoo.com/"
}
Template:
{{{link href=foo title=bar}}}
Result:
<a href="http://yahoo.com/">Yahoo!</a>

Check the code to know used helper codes

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

Handlebars.registerHelper({
  'link': function (options) {
    return '<a href="' + options['hash']['href'] + '">' + options['hash']['title'] + '</a>';
  }
});
var render = Handlebars.compile(template);
var data = {
  "bar": "Yahoo!",
  "foo": "http://yahoo.com/"
};
console.log(render(data));

See Also...