Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Helpers Unavailable When Rendered #347

Open
jasonh-brimar opened this issue Aug 10, 2020 · 0 comments
Open

Nested Helpers Unavailable When Rendered #347

jasonh-brimar opened this issue Aug 10, 2020 · 0 comments

Comments

@jasonh-brimar
Copy link

jasonh-brimar commented Aug 10, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant