fvsch / kirby-twig

Twig templating support for Kirby CMS 2. For Kirby 3, use https://github.com/amteich/kirby-twig
MIT License
70 stars 8 forks source link

Unable to find templates with WAMP or XAMPP on windows #29

Closed anthonyivol closed 6 years ago

anthonyivol commented 6 years ago

Everything is working well with mamp and on production server. But I got an error with XAMPP or WAMP : Twig_Error_Loader Unable to find template "C:/xampp/htdocs/site/templates/home.twig" (looked into: C:\xampp\htdocs\site\templates).

it seems that there is something with FileSystem.php line 212

foreach ($this->paths[$namespace] as $path) {
         if (!$this->isAbsolutePath($path)) {
                $path = $this->rootPath.'/'.$path;
            }
           if (is_file($path.'/'.$shortname)) {
                if (false !== $realpath = realpath($path.'/'.$shortname)) {
                    return $this->cache[$name] = $realpath;
                }
                return $this->cache[$name] = $path.'/'.$shortname;
            }
        }

I thought it was because of slashes and anti-slashes but, if I change :

is_file($path.'/'.$shortname)

with :

is_file($shortname)

and

return $this->cache[$name] = $path.'/'.$shortname;

with :

return $this->cache[$name] = $shortname;

It finds the home.twig but than can't find snippets or all included twig templates in my home.twig file.

Any suggestions would be appreciate ! Thanks

fmathey commented 6 years ago

Hi, i have the same problem. Have you find a solution ?

Thanks ! :-)

fvsch commented 6 years ago

Sadly I don't have access to Windows to test, these days. I’ll have to test in a Windows VM, but I don't know when I will have time to set it up.

If anyone feels comfortable debugging what is happening, that would be great.

I don't think the error is in FileSystem.php line 212 (the part that sets up Twig namespaces like @templates and @snippets). It's more likely when we're trying to figure out the template path to pass to Twig's main render function.

I suspect this part could be at fault: https://github.com/fvsch/kirby-twig/blob/master/src/TwigEnv.php#L206-L209

In renderPath, it would be helpful to test something like this:

// Remove the start of the templates path, since Twig asks
// for a path starting from one of the registered directories.
$path = ltrim(str_replace($this->templateDir, '', preg_replace('#[\\\/]+#', '/', $filePath)), '/');

var_dump([
  'templateDir' => $this->templateDir,
  'filePath' => $filePath,
  'path' => $path
]);
exit;

(Adding the var_dump after the $path = … line.)

@fmathey, could you perhaps try it and report what it prints?

mhuegel commented 6 years ago

@fvsch here's what it prints:

C:\ampps\www\moby\site\plugins\twig\src\TwigEnv.php:217:
array (size=3)
  'templateDir' => string 'C:\ampps\www\moby\site\templates' (length=32)
  'filePath' => string 'C:/ampps/www/moby/site/templates/default.twig' (length=45)
  'path' => string 'C:/ampps/www/moby/site/templates/default.twig' (length=45)

I'm using ampps on windows and got the same problem as the guys above.

mhuegel commented 6 years ago

Honestly i do not really know what i'm doing right here, but this line of code:

$path = substr(strrchr(rtrim($filePath, '/'), '/'), 1);

fixed it for me, at least for the moment.

fvsch commented 6 years ago

Thanks for the debug info @mhuegel, I think this commit should fix the issue.

Do you think you could test by installing the zip from the bugfix branch? Download: https://github.com/fvsch/kirby-twig/archive/bugfix-29.zip

mhuegel commented 6 years ago

@fvsch tried it with a quick default.twig and an included snippet and it works just fine! Thank you for the fix!