Twig只渲染/显示视图路径


Twig renders / display only the view path

我正在创建一个用于学习/教学目的的mvc结构,到目前为止,我可以设置结构和控制器加小枝作为模板系统。

结构为:

  • index . php
  • 控制器/
    • error.php
  • 公司/
    • controller_base.php
    • view_manager.php
  • 视图/
    • .cache/
    • 误差
    • /
      • view.html

:

  • index实例化树枝自动加载器(和mvc自动加载器的spl_register)。
  • index实例化错误控制器继承controller_base。
  • controller_base持有view_manager。

  • 错误调用view_manager显示error/view.html,我在浏览器上得到的唯一东西是error/view.html

apache日志中没有错误。(error_reporting(E_ALL))
小枝缓存文件创建正确,但内容看起来不太好我:

protected function doDisplay(array $context, array $blocks = array()) {
    // line 1
    echo "error/view.html";
}

谁知道为什么,以及如何打印实际视图?
提前谢谢。

代码:

function __autoload($class_name)
{
if(file_exists("controllers/$class_name.php")): 
    include strtolower("controllers/$class_name.php");
elseif(file_exists("models/$class_name.php")):
    include strtolower("models/$class_name.php");
elseif(file_exists("inc/$class_name.php")):
    include strtolower("inc/$class_name.php");
endif;
}
spl_autoload_register('__autoload');
require_once 'vendor/autoload.php';

Twig_Autoloader:注册();已经避免了,因为Twig安装是由作曲家完成的。添加它不会带来任何变化。

error.php (controller): called method.

public function show($param)
{  
    $this->viewMng->display(get_class().$data['view'], array())
}

controller_base.php:

class base
{
    protected $viewMng;
    public function __construct()
    {
        $this->viewMng = new viewmanager();
        }
}

viewmanager.php:整个类

class viewmanager {
    private $twig;
    protected $template_dir = 'views/';
    protected $cache_dir = 'views/.cache';
//  protected $vars = array();
    public function __construct($template_dir = null) {
        if ($template_dir !== null) {
            // Check here whether this directory really exists
            $this->template_dir = $template_dir;
        }
        $loader = new Twig_Loader_String($this->template_dir);
        $this->twig = new Twig_Environment($loader, array(
                        'cache' => $this->cache_dir));
    }
    public function render($template_file, $data = array()) {
        if (!file_exists($this->template_dir.$template_file)) {
            throw new Exception('no template file ' . $template_file . ' present in directory ' . $this->template_dir);
        }
        return $this->twig->render($template_file, $data);
    }
    public function display($template_file, $data) {
        if (!file_exists($this->template_dir.$template_file)) {
            throw new Exception('no template file ' . $template_file . ' present in directory ' . $this->template_dir);
        }
        $tmpl = ($this->twig->loadTemplate($template_file));//print_r($tmpl);
        $tmpl->display($data);
    }
}

view.html:

<html><body> Hello </body></html>

问题是基于加载器。
根据Twig文档:

Twig_Loader_String从字符串中加载模板。这是一个假装填机作为模板引用的是模板源代码
This加载器应该只用于单元测试,因为它有严重的缺陷限制:一些标签,如extends或include没有意义要用作模板引用的是模板源代码本身。

这就是为什么它只输出传入的字符串。
Twig_Loader_String,应该替换为合适的加载器。
在这种情况下,它工作得很好Twig_Loader_Filesystem。

$loader = new Twig_Loader_Filesystem($this->template_dir);

这就解决了问题,MVC结构完全可以正常工作。
谢谢大家看一看