Heim  >  Artikel  >  php教程  >  框架的view类

框架的view类

WBOY
WBOYOriginal
2016-06-06 19:34:051274Durchsuche

简单的框架view类,用于视图处理 无 ?php/***视图文件***/class View {private $_variables = array();//参数列表private $_useLayout = true;//使用公共魔板private $_templateType = 'phtml';private $_route;private $_layout;public function __construct

简单的框架view类,用于视图处理
<?php
/**
*视图文件
*
**/
class View {
private $_variables = array();//参数列表
private $_useLayout = true;//使用公共魔板
private $_templateType = 'phtml';
private $_route;
private $_layout;

public function __construct($route)
{
$this->_route = $route;
$this->_layout = APP_PATH.DS.'layouts'.DS.'main.'.$this->_templateType;
}

public function __set($name,$value)
{
$this->_variables[$name] = $value;
}

public function __get($name)
{
return $this->_variables[$name];
}

public function noLayout()
{
        $this->_useLayout = false;
}

public function setTemplateType($templatType)
{
        $this->_templateType = $templatType;
}
    
public function setLayout($layout,$path='')
{
       $path == '' && $path = APP_PATH.DS.'layouts';
       $this->_layout = $path.DS.$layout.'.'.$this->_templateType;
}
public function render($template = '')
{
        if(!$template)
           $template = $this->_route->getAction();
$module = $this->_route->getModule();
$templateFile = APP_PATH.DS.($module?'modules'.DS.$module.DS:'').'views'.DS.$this->_route->getController().DS.$template.'.'.$this->_templateType;
$this->_variables && extract($this->_variables,EXTR_OVERWRITE);
if($this->_useLayout)
{
include $this->_layout;
}
else
{
include $templateFile;
}
}
}
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn