phpsymfonysymfony4symfony-components

You have requested a non-existent parameter "name" error when trying to load a template using Symfony templating component


I am using the Symfony templating component to add templating functionality to the project I am working on, I am following the docs here but I am using the service container Symfony component and adding the code in the docs using this code:

$containerBuilder->register('template_name_parser', Symfony\Component\Templating\TemplateNameParser::class);
$containerBuilder->register('file_system_loader', Symfony\Component\Templating\Loader\FilesystemLoader::class)
    ->setArguments([realpath('./') . '/app/Views/%name%']);
$containerBuilder->register('templating', Symfony\Component\Templating\PhpEngine::class)
    ->setArguments([new Reference('template_name_parser'), new Reference('file_system_loader')]);

But when I try to load a template file using this code:

container->get('templating')->render('home.php')

I get this error:

Something went wrong! (You have requested a non-existent parameter "name".)


Solution

  • So as @yceruto mentioned Symfony DI will treat strings between %% as parameters so to escape it we need to add % in front of those strings as mentioned here, so I updated my code like this:

    $containerBuilder->register('file_system_loader', Symfony\Component\Templating\Loader\FilesystemLoader::class)
    ->setArguments([realpath('./') . '/app/Views/%%name%']);