Skip to content

Issue when using "traditional" path notation #194

@emulienfou

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;
    }
}

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