简体   繁体   English

Twig即使存在也无法找到模板

[英]Twig is unable to find the template even though it exists

This is my config.php file located in root directory of my site: 这是我的config.php文件,位于我网站的根目录中:

<?php

    define('SITE_ROOT',  dirname(__FILE__));
    define('CORE_DIR',   SITE_ROOT . '/core');
    define('VIEWS_DIR',  SITE_ROOT . '/views');
    define('ASSETS_DIR', SITE_ROOT . '/assets');

    require SITE_ROOT . '/vendor/autoload.php';
    require CORE_DIR . '/DBConnector.php';

    $loader = new Twig_Loader_Filesystem(VIEWS_DIR);
    $twig = new Twig_Environment($loader);

    function loadHead($title) {

        global $twig;

        echo $twig->render(VIEWS_DIR . '/head.html', array('naslov' => $title));

    }

SITE_ROOT is /Applications/XAMPP/xamppfiles/htdocs/site_name where config.php is located too and head.html is located in views folder. SITE_ROOT/Applications/XAMPP/xamppfiles/htdocs/site_name ,其中config.php也位于其中,而head.html位于views文件夹中。

在此处输入图片说明

However, I'm still getting error whenever I try to access any page in site root directory. 但是,每当尝试访问站点根目录中的任何页面时,我仍然会收到错误消息。

Fatal error: Uncaught Twig_Error_Loader: Unable to find template "/Applications/XAMPP/xamppfiles/htdocs/site_name/views/head.html" (looked into: /Applications/XAMPP/xamppfiles/htdocs/site_name/views).

So far, I tried to restart Apache server, checked directory two times to ensure that file is actually there (it is and I can access it directly via browser), checked folder permissions and cleared the cache but nothing worked. 到目前为止,我尝试重新启动Apache服务器,两次检查目录以确保文件确实存在(可以通过浏览器直接访问该文件),检查文件夹权限并清除了缓存,但没有任何效果。

Twig相对于传递的view(s)文件夹定位模板,在这种情况下,您将该文件夹设置为/Applications/XAMPP/xamppfiles/htdocs/site_name/views ,这意味着渲染head的正确代码为

$twig->render('head.html', ...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM