【发布时间】:2012-11-09 03:23:06
【问题描述】:
我已经阅读了很多次文章,但我仍然无法理解某些部分。 文章链接:Model-View-Confusion part 1: Why the model is accessed by the view in MVC
下面的代码是我认为我很困惑的代码。
class ListView extends View {
public $model;
public $template;
public $listTemplate;
public $errorTemplate;
public $itemName = 'items';
public function output() {
$result = $this->model->findAll();
if (count($result) > 0) {
$this->template = $this->getTemplate($this->listTemplate);
$this->template->addSet($this->itemName, $result);
} else {
$this->template = $this->getTemplate($this->errorTemplate);
}
return $this->template->render();
}
}
控制器看起来像这样:
class UserController extends Controller {
public $viewName = 'ListView';
public function showList() {
$this->view->model = $this->model->user;
$this->view->listTemplate = 'UserList.tpl';
$this->view->errorTemplate = 'ErrorNoUsers.tpl';
}
}
据我所知,template 被分配给从名为 getTemplate 的 View 继承的方法的结果,该方法通过来自 View 的方法再次命名为 listTemplate
像这样$this->getTemplate($this->listTemplate)
我感到困惑的是$template突然有了一个方法,这意味着它变成了一个类。就在这里$this->template->addSet($this->itemName, $result); 和`$this->template->render();
你知道那里发生了什么吗?
【问题讨论】:
-
调用
$this->getTemplate($this->listTemplate);会返回一个对象实例。我怀疑Template类可能类似于this 示例。
标签: model-view-controller views controllers