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

Render-time partials cannot access @partial-block. #341

Open
jasonh-brimar opened this issue Apr 25, 2020 · 0 comments
Open

Render-time partials cannot access @partial-block. #341

jasonh-brimar opened this issue Apr 25, 2020 · 0 comments

Comments

@jasonh-brimar
Copy link

While working on new a problem in my application that I traced back to #292, I found a simpler test case which exhibits just one of the several bug present in that issue. I thought it would be good to create a separate issue for this bug because it can be fixed separately from the others present in the referenced issue.

In this test case, a very simple usage of partial blocks fails to work in render-time partials:

  1. Rendered by LightnCandy 1.2.5 (Compile-Time Partials):

    Jason says: “This text was sent from the template to the partial.”

  2. Rendered by LightnCandy 1.2.5 (Render-Time Partials):

    Jason says: “”

  3. Rendered by LightnCandy 1.2.5 (Compile- & Render-Time Partials):

    Jason says: “This text was sent from the template to the partial.”

  4. Rendered by Handlebars.js 4.7.6:

    Jason says: “This text was sent from the template to the partial.”

The second scenario (render-time partials only) also emits a “Can not find partial named as '@partial-block' !!” message to my error log.

Here is the code I used to generate those results:

require('./vendor/autoload.php');

// Define the template string.
$templateString = '{{#> MyPartial child}}This <b>text</b> was sent from the template to the partial.{{/MyPartial}}';

// Define the partial template strings.
$partialTemplateString = '{{name}} says: “{{> @partial-block }}”';

// Define the LightnCandy compile options, with and without partials.
$compileOptionsWithoutPartials = [
    'flags' => LightnCandy\LightnCandy::FLAG_HANDLEBARSJS_FULL | LightnCandy\LightnCandy::FLAG_ERROR_SKIPPARTIAL | LightnCandy\LightnCandy::FLAG_EXTHELPER | LightnCandy\LightnCandy::FLAG_ERROR_EXCEPTION
];
$compileOptionsWithPartials = array_merge($compileOptionsWithoutPartials, [
    'partials' => [
        'MyPartial' => $partialTemplateString,
    ]
]);

// Create the template using LightnCandy, with and without partials.
$templateWithPartials = eval(LightnCandy\LightnCandy::compile($templateString, $compileOptionsWithPartials));
$templateWithoutPartials = eval(LightnCandy\LightnCandy::compile($templateString, $compileOptionsWithoutPartials));

// Define the LightnCandy render options, with and without partials.
$renderOptionsWithoutPartials = [];
$renderOptionsWithPartials = array_merge($renderOptionsWithoutPartials, [
    'partials' => [
        'MyPartial' => eval('use \LightnCandy\Runtime as LR; use \LightnCandy\SafeString as SafeString; return ' . LightnCandy\LightnCandy::compilePartial($partialTemplateString, $compileOptionsWithoutPartials) . ';'),
    ]
]);

// Create a context.
$context = new stdClass();
$context->child = new stdClass();
$context->child->name = 'Jason';

?>
<!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <title>Partial Block Test</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <style>
            #php-compile-partials, #php-render-partials, #php-both-partials, #js {
                border: 1px solid black;
                margin: 20px;
                padding: 20px;
            }
            #php-compile-partials {
                background: #ffe;
            }
            #php-render-partials {
                background: #fef;
            }
            #php-both-partials {
                background: #eef;
            }
            #js {
                background: #eff;
            }
        </style>
    </head>

    <body>

        <div id="php-compile-partials">
            <h1>Rendered by LightnCandy 1.2.5 (Compile-Time Partials)</h1>
            <div id="render-php-compile-partials"><?= $templateWithPartials($context, $renderOptionsWithoutPartials) ?></div>
        </div>

        <div id="php-render-partials">
            <h1>Rendered by LightnCandy 1.2.5 (Render-Time Partials)</h1>
            <div id="render-php-render-partials"><?= $templateWithoutPartials($context, $renderOptionsWithPartials) ?></div>
        </div>

        <div id="php-both-partials">
            <h1>Rendered by LightnCandy 1.2.5 (Compile- &amp; Render-Time Partials)</h1>
            <div id="render-php-both-partials"><?= $templateWithPartials($context, $renderOptionsWithPartials) ?></div>
        </div>

        <div id="js">
            <h1>Rendered by Handlebars.js 4.7.6</h1>
            <div id="render-js"></div>
        </div>

        <script id="context" type="application/json"><?= json_encode($context) ?></script>

        <script id="parent-template" type="text/x-handlebars-template"><?= $templateString ?></script>
        <script id="partial-template" type="text/x-handlebars-template"><?= $partialTemplateString ?></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.6/handlebars.js" integrity="sha256-ZafrO8ZXERYO794Tx1hPaAcdcXNZUNmXufXOSe0Hxj8=" crossorigin="anonymous"></script>

        <script>

            // Make the partial template available as a partial.
            Handlebars.registerPartial("MyPartial", Handlebars.compile(document.getElementById("partial-template").innerHTML));

            // Compile the template and render with the supplied context.
            document.getElementById("render-js").innerHTML = Handlebars.compile(document.getElementById("parent-template").innerHTML)(JSON.parse(document.getElementById("context").innerHTML));

        </script>

    </body>

</html>
@jasonh-brimar jasonh-brimar changed the title Render-Time Partials Render-time partials cannot access @partial-block. Apr 25, 2020
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