Php 细枝仅渲染/显示视图路径
我正在为学习/教学目的创建一个mvc结构,到目前为止,我可以设置该结构和一个控制器加上细枝作为模板系统 结构如下:Php 细枝仅渲染/显示视图路径,php,model-view-controller,twig,Php,Model View Controller,Twig,我正在为学习/教学目的创建一个mvc结构,到目前为止,我可以设置该结构和一个控制器加上细枝作为模板系统 结构如下: index.php 控制器/ error.php 公司/ controller_base.php 查看\u manager.php 视图/ .缓存/ 错误/ view.html 所以: 索引实例化细枝自动加载器(和通过spl_寄存器的mvc自动加载器)。 索引实例化继承控制器\u基的错误控制器。 控制器\u基座正在容纳视图\u管理器。 error调用view\
- index.php
- 控制器/
- error.php
- error.php
- 公司/
- controller_base.php
- 查看\u manager.php
- controller_base.php
- 视图/
- .缓存/
- 错误/
- view.html
- view.html
- 索引实例化细枝自动加载器(和通过spl_寄存器的mvc自动加载器)。
- 索引实例化继承控制器\u基的错误控制器。
- 控制器\u基座正在容纳视图\u管理器。
- error调用view\u manager显示
,我在浏览器上得到的唯一信息是错误/view.html
错误/view.html
错误报告(E\u ALL)
)细枝缓存文件创建正确,但内容在我看来不太好:
protected function doDisplay(array $context, array $blocks = array()) {
// line 1
echo "error/view.html";
}
有人知道为什么以及如何打印实际视图吗?提前谢谢 代码:
index.php:声明自动加载程序
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';
细枝自动加载器::寄存器();已避免,因为细枝安装是由composer完成的。
添加它不会带来任何变化
error.php(controller):调用的方法
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文档:
细枝加载程序\字符串从字符串加载模板。这是一个虚拟装载机 因为模板引用是模板源代码
装载机只能用于单元测试,因为它具有严重的 限制:一些标记,如extends或include,没有意义 用作模板引用的是模板源代码 本身。
这就是为什么它只打印传入的字符串。
细枝加载器\字符串,应替换为正确的加载器。
在这种情况下,它在细枝加载器文件系统中工作得非常好。
$loader = new Twig_Loader_Filesystem($this->template_dir);
这就解决了问题,MVC结构工作完全正常。谢谢大家看一看。你们能发布“错误调用视图管理器以显示错误/view.html”的代码吗?对不起,我已经添加了所有相关的代码。如果你还想看别的东西,就问吧。谢谢你看!调用
echo$this->viewMng->render(get_class().$data['view'],array())
时会发生什么?还有为什么会有括号:tmpl=($this->twig->loadTemplate($template_file))代码>?渲染函数产生完全相同的结果。没有理由使用括号。可能是编辑和尝试不同选项的结果。他们肯定会在最后离开。还尝试将模板放置在视图根文件夹中,结果相同。这不是路径上的问题,否则会产生异常。编辑问题:doDisplay在“编译文件”中只包含“echo”error/view.html“;”。不知道这是否正常,但可能有助于解决问题。