/**
* 绑定数据到视图
*/
class ViewModel
{
/**
* 保存赋给视图模板的变量
*/
private $data = array();
/**
* 保存视图渲染状态
*/
private $render = FALSE;
/**
* 加载一个视图模板
*/
public function __construct($template)
{
//构成完整文件路径
$file = 'Home/View/'.strtolower($template) . '.php';
if (file_exists($file))
{
/**
* 当模型对象销毁时才能渲染视图
* 如果现在渲染视图,就不能给视图模板赋予变量
* 所以此处先保存要渲染的视图文件路径
*/
$this->render = $file;
}
}
/**
* 将控制器赋予的变量,并保存在data数组中
*
* @param $variable
* @param $value
*/
public function assign($variable , $value)
{
$this->data[$variable] = $value;
}
public function display() {
//把类中的data数组变为该函数的局部变量,便于视图模板中使用
$data = $this->data;
//渲染视图
include($this->render);
}
}
?>
一键复制
编辑
Web IDE
原始数据
按行查看
历史