You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
require('./vendor/autoload.php');
// Define the template string.// $templateString = '<!--{{isEqual "a" "a"}}-->{{#if (isEqual "a" "a")}}equal{{else}}not equal{{/if}}'; // This works.// $templateString = '{{#if (isEqual "a" "a")}}equal{{else}}not equal{{/if}}<!--{{isEqual "a" "a"}}-->'; // This works, too.$templateString = '{{#if (isEqual "a" "a")}}equal{{else}}not equal{{/if}}'; // This does NOT work.// Define the LightnCandy render options.$renderOptions = [
'helpers' => [
'isEqual' => (
function ($a, $b): bool
{
return$a === $b;
}
)
]
];
// Create the template using LightnCandy.$template = eval(LightnCandy\LightnCandy::compile($templateString, [
'flags' => LightnCandy\LightnCandy::FLAG_HANDLEBARSJS_FULL | LightnCandy\LightnCandy::FLAG_ERROR_SKIPPARTIAL | LightnCandy\LightnCandy::FLAG_EXTHELPER | LightnCandy\LightnCandy::FLAG_ERROR_EXCEPTION,
'helperresolver' => (
function ($context, $name) use ($renderOptions): bool
{
returnarray_key_exists($name, $renderOptions['helpers']);
}
)
]));
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Non-String Helper Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style>
#php, #js {
border: 1px solid black;
margin: 20px;
padding: 20px;
}
#php {
background: #ffe;
}
#js {
background: #eff;
}
</style>
</head>
<body>
<div id="php">
<h1>Rendered by PHP</h1>
<div id="rendered-php"><?=$template(newstdClass(), $renderOptions) ?></div>
</div>
<div id="js">
<h1>Rendered by JS</h1>
<div id="rendered-js"></div>
</div>
<script id="template" type="text/x-handlebars-template"><?=$templateString?></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.6/handlebars.js" integrity="sha256-ZafrO8ZXERYO794Tx1hPaAcdcXNZUNmXufXOSe0Hxj8=" crossorigin="anonymous"></script>
<script>
// Register helpers...
Handlebars.registerHelper({
isEqual: function (a, b, options) {
return a === b;
}
});
// Extract the template from the the DOM and render.
document.getElementById("rendered-js").innerHTML = Handlebars.compile(document.getElementById("template").innerHTML)({});
</script>
</body>
</html>
The Issue:
Handlebars generates an output of equal. LightnCandy generates an output of not equal.
Two alternative templates are provided in the PHP code above. They include a non-nested usage of the isEqual helper before or after the nested usage. Both of those templates generate consistent output between LightnCandy and Handlebars.
These tests show that helpers are only made available if they are used in at least one non-nested location within the template. To match Handlebars, helpers should need to be made available even if they are used exclusively in nested locations within a template.
I believe this is the root cause behind #293 and #294, so if this issue is fixed then I will confirm whether those are also fixed.
The text was updated successfully, but these errors were encountered:
The PHP Code:
The Issue:
Handlebars generates an output of
equal
. LightnCandy generates an output ofnot equal
.Two alternative templates are provided in the PHP code above. They include a non-nested usage of the
isEqual
helper before or after the nested usage. Both of those templates generate consistent output between LightnCandy and Handlebars.These tests show that helpers are only made available if they are used in at least one non-nested location within the template. To match Handlebars, helpers should need to be made available even if they are used exclusively in nested locations within a template.
I believe this is the root cause behind #293 and #294, so if this issue is fixed then I will confirm whether those are also fixed.
The text was updated successfully, but these errors were encountered: