Specification of multiple Twig paths in a yaml getting ignored

216 Views Asked by At

My twig.yaml file in a nearly fresh Bolt CMS (Symfony 5.4) project looks like this ...

twig:
    ...
    paths:
        '%kernel.project_dir%/public/theme/%bolt.theme%': 'theme'        
        '%kernel.project_dir%/vendor/bolt/core/templates/': 'bolt'
        '%kernel.project_dir%/src/templates': 'templates'
        '%kernel.project_dir%/vendor/bolt/core/templates': ''

... and my controller looks like this when it "works" ...

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
    #[Route('/default', name: 'app_default')]
    public function index(): Response
    {
        return $this->render('default/index.html.twig', [
            'controller_name' => 'DefaultController',
        ]);
    }
}

... but when I change default/index.html.twig in my controller to say myplace/index.html.twig instead, I find that my custom template does not load. Instead, I get this ...

Unable to find template "myplace/index.html.twig" (looked into: /path/to/project/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views, /path/to/project/vendor/bolt/core/templates, /path/to/project/vendor/symfony/twig-bridge/Resources/views/Form).

Now here's the interesting part: When I put the following into twig.yaml ...

twig:
    ...
    paths:
        '%kernel.project_dir%/public/theme/%bolt.theme%': 'theme'        
        '%kernel.project_dir%/vendor/bolt/core/templates/': 'bolt'
        '%kernel.project_dir%/src/templates': ''
        '%kernel.project_dir%/vendor/bolt/core/templates': 'templates'

... my new template in src/templates/myplace starts showing up in the browser. At that point, of course, Bolt's default Twig templates stop working, meaning I can't use the CMS.

How can I make both sets of templates findable by Symfony?

EDIT: I thought maybe templates was some sort of reserved word, so I tried changing the name assigned to src/templates in twig.yaml from templates to bob. That did not do the trick, unfortunately. Obviously, myplace exists, because the template is rendered correctly when I remove the key. It's just that having a key in there somehow causes the template resolution to fail.

1

There are 1 best solutions below

0
Mayor of the Plattenbaus On

Well ... that was embarrassingly simple. I just had to remove the values assigned to both of the locations via my twig.yaml file. So that yaml now looks like this:

twig:
    ...
    paths:
        '%kernel.project_dir%/public/theme/%bolt.theme%': 'theme'        
        '%kernel.project_dir%/vendor/bolt/core/templates/': 'bolt'
        '%kernel.project_dir%/src/templates': ''
        '%kernel.project_dir%/vendor/bolt/core/templates': 'templates'

I'm honestly not sure why bolt is assigning values ('theme' and 'bolt') to these path keys in the first place, since it seems to cause things to break when I do it elsewhere in the same yaml file. Maybe a bolt cms expert can chime in on that. For now, I'm not touching what bolt has preconfigured, out of FUD. I'll just leave those values there in case removing them breaks something, praying feverishly to Winnie the Pooh that all things seen and unseen will be well forevermore.