Skip to content

Unable to resolve partial when using sub expression and partial resolver #5

@eliashaeussler

Description

@eliashaeussler

Hi!

I get an exception when using sub expressions (lookup) within a partial call in combination with a configured partialResolver. Let me sum up things a little bit.

Failing example: Dynamic lookup with configured partial resolver

Given the following template (template-1.hbs):

{{> (lookup . 'partialName')}}

And the following partial (partial.hbs):

Hello from the partial!

And this is my PHP code (test-1.php):

$template = \DevTheorem\Handlebars\Handlebars::compile(
    file_get_contents(__DIR__ . '/template-1.hbs'),
    new \DevTheorem\Handlebars\Options(
        partialResolver: static function (\DevTheorem\Handlebars\Context $context, string $name) {
            return file_get_contents(__DIR__ . '/' . $name . '.hbs');
        },
    ),
);

echo $template([
    'partialName' => 'partial',
]);

I receive the following exception:

$ php test-1.php
PHP Fatal error:  Uncaught Exception: Runtime: the partial partial could not be found in /xxx/vendor/devtheorem/php-handlebars/src/Runtime.php:293
Stack trace:
#0 /xxx/vendor/devtheorem/php-handlebars/src/Handlebars.php(41) : eval()'d code(16): DevTheorem\Handlebars\Runtime::p(Object(DevTheorem\Handlebars\RuntimeContext), 'partial', Array, 0, '')
#1 /xxx/test-1.php(14): DevTheorem\Handlebars\Handlebars::{closure}(Array)
#2 {main}
  thrown in /xxx/vendor/devtheorem/php-handlebars/src/Runtime.php on line 293

Fatal error: Uncaught Exception: Runtime: the partial partial could not be found in /xxx/vendor/devtheorem/php-handlebars/src/Runtime.php:293
Stack trace:
#0 /xxx/vendor/devtheorem/php-handlebars/src/Handlebars.php(41) : eval()'d code(16): DevTheorem\Handlebars\Runtime::p(Object(DevTheorem\Handlebars\RuntimeContext), 'partial', Array, 0, '')
#1 /xxx/test-1.php(14): DevTheorem\Handlebars\Handlebars::{closure}(Array)
#2 {main}
  thrown in /xxx/vendor/devtheorem/php-handlebars/src/Runtime.php on line 293

Working example 2: Dynamic lookup without (!) partial resolver

Same template like example 1, but a different PHP code:

$template = \DevTheorem\Handlebars\Handlebars::compile(
    file_get_contents(__DIR__ . '/template-2.hbs'),
    new \DevTheorem\Handlebars\Options(
        partials: [
            'partial' => file_get_contents(__DIR__ . '/partial.hbs'),
        ],
    ),
);

echo $template([
    'partialName' => 'partial',
]);

This works as expected:

$ php test-2.php
Hello from the partial!

Working example 3: Fixed partial name with configured partial resolver

Template:

{{> partial}}

PHP code:

$template = \DevTheorem\Handlebars\Handlebars::compile(
    file_get_contents(__DIR__ . '/template-3.hbs'),
    new \DevTheorem\Handlebars\Options(
        partialResolver: static function (\DevTheorem\Handlebars\Context $context, string $name) {
            return file_get_contents(__DIR__ . '/' . $name . '.hbs');
        },
    ),
);

echo $template();

This works as expected:

$ php test-3.php
Hello from the partial!

Working example 4: Fixed partial without (!) partial resolver

Same template like example 3, but a different PHP code:

$template = \DevTheorem\Handlebars\Handlebars::compile(
    file_get_contents(__DIR__ . '/template-4.hbs'),
    new \DevTheorem\Handlebars\Options(
        partials: [
            'partial' => file_get_contents(__DIR__ . '/partial.hbs'),
        ],
    ),
);

echo $template();

This works as expected:

$ php test-4.php
Hello from the partial!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions