XaminProject/handlebars.php — pull request #35 by cgray (2014-01-16 20:23:52+00:00)
Title: Update Context::get to resolve @key and @index

Change to allow @key and @index to be resolved within Context::get calls.

For Example to port this javascript helper
Handlebars.registerHelper("ifEquals", function (name, value,options){
if (name == value) {
return options.fn(this)
};
});

Helpers that could accept @key and @index as parameters previously had to implemented like

$handleBars->addHelper("ifEquals", function ($template, $context, $args, $source){
list($property, $value) = explode(" ", $args);
if ($property == "@index"){
$property = $context->lastIndex();
} else if ($property == "@key") {
$property = $context->lastKey();
} else {
$property = $context->get($value);
}
if ($value == "@index"){
$value = $context->lastIndex();
} else if ($property == "@key") {
$value = $context->lastKey();
} else {
$value = $context->get($value);
}
if ($property == $value){
return $template->render($context);
}
});

Can Now be implemented as

$handleBars->addHelper("ifEquals", function ($template, $context, $args, $source){
list($property, $value) = explode(" ", $args);
if ($context->get($property) == $context->get($value)){
return $template->render($context);
}
});