Skip to content

Possible to support providing callables as array members in context? #4757

Open

Description

@msbit

With the following setup:

use Twig\Environment;
use Twig\Loader\ArrayLoader;

$twig = new Environment(
    new ArrayLoader([
        'test' => '{{ test.test() }}',
    ]), 
);
$twig->enableStrictVariables();
$template = $twig->load('test');

attempting the following will succeed:

$template->render([
    'test' => new class {
        public function test()
        {   
            return 'test';
        }   
    },  
]);

while the following will fail:

$template->render([
    'test' => [
        'test' => fn() => 'test',
    ],
]);

I am motivated to try the second one in tests, to avoid having to create complicated objects which might be tricky to do in an isolated way.

I'm curious to know if this is a design decision, or a niche which hasn't been popular enough to implement. I note the existence of two related issues:

but I'm not sure if they cover this case, or distinct ones.

Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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