Skip to content

Overriding templates does not work if Twig's default path uses %env()% placeholders #40025

Closed
@Wirone

Description

@Wirone

Symfony version(s) affected: 5.2.1 (but probably earlier too)

Description
Overriding bundles' templates does not work with custom twig.default_path. I wanted to use custom error pages and struggled to get it to work because even if bundles/TwigBundle/Exception/error.html.twig existed under path configured as default, it wasn't loaded.

It looks like @Twig namespace is registered magically based on bundles/TwigBundle existance, but it works properly only with default twig.default_path.

How to reproduce

  • change twig.default_path to any path that uses %env()% placeholder
  • put bundles/TwigBundle/Exception/error.html.twig there
  • run bin/console debug:twig @Twig/Exception/error.html.twig

Result:

Matched File
------------
                                                                                                                        
 Template name "@Twig/Exception/error.html.twig" not found                                                              
                                                                                                                        
Configured Paths
----------------
                                                                                                                        
 No template paths configured for "@Twig" namespace  

Possible Solution
Not known, I don't know how those namespaces are registered right now.

Additional context
I've changed Twig's default path because I use Encore with multiple packages (support for themes), so in the end default path for Twig depends on configured theme. Error pages also must be a part of theme, so overriding templates must be resolved inside dynamically constructed path (default_path: '%kernel.project_dir%/templates/%env(APP_THEME)%')

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions