Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when using "traditional" path notation #194

Open
emulienfou opened this issue Oct 5, 2018 · 3 comments
Open

Issue when using "traditional" path notation #194

emulienfou opened this issue Oct 5, 2018 · 3 comments

Comments

@emulienfou
Copy link
Contributor

emulienfou commented Oct 5, 2018

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;
    }
}
@xabbuh
Copy link
Contributor

xabbuh commented Oct 5, 2018

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

@danrot
Copy link
Contributor

danrot commented Oct 5, 2018

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?

@emulienfou
Copy link
Contributor Author

emulienfou commented Oct 5, 2018

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants