Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

Issue when using "traditional" path notation #194

Open

Description

@emulienfou

Preconditions

When using the traditional notation for template path, the file is not found.

Steps to reproduce

  • Template not found when using traditional notation of template path, like CmfTreeBrowserBundle:Base:scripts.html.twig.
  • If I change the path to the new notation, like: @CmfTreeBrowser/Base/scripts.html.twig this solve the issue.
  • However, some bundles still use the traditional notation and I think this need to be fixed directly in this bundle.

Possible fix

Here is a fix/test I did to not have this issue.
I would loved to have some feedback and if you can think about a better handling about that.

<?php
// ...
class FilesystemLoader extends \Twig_Loader_Filesystem
{
//...
protected function findTemplate($template, $throw = true)
    {
        $logicalName = (string) $template;

        if ($this->activeTheme) {
            $logicalName .= '|'.$this->activeTheme->getName();
        }

        if (isset($this->cache[$logicalName])) {
            return $this->cache[$logicalName];
        }

        $file = null;
        $previous = null;

        try {
            $templateReference = $this->parser->parse($template);
            $file = $this->locator->locate($templateReference);

        } catch (\Exception $e) {
            $previous = $e;

            // for BC
            try {
                $file = parent::findTemplate((string) $template);
            } catch (\Twig_Error_Loader $e) {
                $previous = $e;

               // Traditional notation need to be replaced by the new notation
                try {
                    $template = (string) $template;
                    $template = str_replace('Bundle:', '/', $template);
                    $template = str_replace(':', '/', $template);
                    $file = parent::findTemplate('@' . $template);
                } catch(\Twig_Error_Loader $e){
                    $previous = $e;
                }
            }
        }

        if (false === $file || null === $file) {
            if ($throw) {
                throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous);
            }
            
            return false;
        }

        return $this->cache[$logicalName] = $file;
    }
}

Activity

  1. xabbuh commented on Oct 5, 2018

    @xabbuh
    Contributor

    I think that's related to #193 (comment).

  2. danrot commented on Oct 5, 2018

    @danrot
    Contributor

    Tracked it even further down: #193 (comment)

    The TemplateNameParser class seems to be still available in Symfony 4.1, any reason not to use that one?

  3. emulienfou commented on Oct 5, 2018

    @emulienfou
    ContributorAuthor

    Hello @danrot you're right the next code is the issue even in SF4.1.6:

    $twigFilesystemLoaderDefinition->setArguments(array(
        $container->getDefinition('liip_theme.templating_locator'),
        $container->getDefinition('templating.filename_parser')
    ));

    I will need to do some other tests on the main application where I have the issue and had to add this code before confirming if removing this code fix the problem in all cases.

    So, the "fix/update" in did above can be removed/ignored because it works without it

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