如何配置Twig来查找模板?

时间:2016-10-16 18:08:34

标签: php twig slim

我正在使用Twim和Slim,我收到以下错误:

  

警告:file_get_contents(application / templates / config.html):失败   打开流:没有这样的文件或目录   /var/www/testing/vendor/twig/twig/lib/Twig/Loader/Filesystem.php on   第131行

以下脚本位于/var/www/testing/html/index.php,我已验证该模板位于/var/www/testing/application/templates/config.html

$container['view'] = function ($c) {
    $view = new \Slim\Views\Twig('../application/templates', [
        //'cache' => 'path/to/cache'    // See auto_reload option
        'debug' => true,
        'strict_variables'=> true
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $c['router'],
        $c['request']->getUri()
    ));
    $view->addExtension(new \Twig_Extension_Debug());
    return $view;
};

$app->get('/config', function (Request $request, Response $response) {
    return $this->view->render($response, 'config.html',[]);
});

第131行显示如下,并返回config.html

public function getSource($name)
{
    return file_get_contents($this->findTemplate($name));
}

我在另一个类似的服务器中使用了相同的脚本(但是,可能不同的PHP版本和php.ini以及httpd.conf可能不同),并且没有这个问题?

显然,我已经错误地配置了一些东西。我应该如何配置Twig来查找模板?

1 个答案:

答案 0 :(得分:2)

是的,这是一个问题(错误?)我上周遇到过,@ geggleto解决了它。

这是因为来自v1.26.1的{​​{1}}的Twig更新。

这就是Twig在1.24.2(方法1.24.2)中获取的方式:

Twig_Loader_Filesystem::normalizeName

这就是它在protected function normalizeName($name) { return preg_replace('#/{2,}#', '/', str_replace('\\', '/', (string) $name)); } 中抓取文件的方式:

1.26.1

看到private function normalizePath($path) { $parts = explode('/', str_replace('\\', '/', $path)); $isPhar = strpos($path, 'phar://') === 0; $new = array(); foreach ($parts as $i => $part) { if ('..' === $part) { array_pop($new); } elseif ('.' !== $part && ('' !== $part || 0 === $i || $isPhar && $i < 3)) { $new[] = $part; } } return implode('/', $new); } 行?这是破坏相对路径使用的那个。

@geggleto建议使用绝对路径而不是相对路径,并且它起作用:

array_pop($new);

总结:这是因为Twig新版本。