zordius / lightncandy

An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ),
https://zordius.github.io/HandlebarsCookbook/
MIT License
610 stars 76 forks source link

Nested Helpers Unavailable When Rendered #347

Open jasonh-brimar opened 4 years ago

jasonh-brimar commented 4 years ago

The PHP Code:

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
        {
            return array_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(new stdClass(), $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.