mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 15:02:47 +08:00
改进Response类 增加think\response\View类 用于渲染模板响应输出
This commit is contained in:
86
library/think/response/View.php
Normal file
86
library/think/response/View.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Config;
|
||||
use think\Response;
|
||||
use think\View as ViewTemplate;
|
||||
|
||||
class View extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [];
|
||||
protected $vars = [];
|
||||
protected $replace = [];
|
||||
protected $contentType = 'text/html';
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
// 渲染模板输出
|
||||
return ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
|
||||
->fetch($data, $this->vars, $this->replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图变量赋值
|
||||
* @access protected
|
||||
* @param array $vars 模板变量
|
||||
* @return $this
|
||||
*/
|
||||
public function vars($vars = [])
|
||||
{
|
||||
$this->vars = $vars;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板变量赋值
|
||||
* @access public
|
||||
* @param mixed $name 变量名
|
||||
* @param mixed $value 变量值
|
||||
* @return $this
|
||||
*/
|
||||
public function assign($name, $value = '')
|
||||
{
|
||||
if (is_array($name)) {
|
||||
$this->vars = array_merge($this->vars, $name);
|
||||
return $this;
|
||||
} else {
|
||||
$this->vars[$name] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图内容替换
|
||||
* @access public
|
||||
* @param string|array $content 被替换内容(支持批量替换)
|
||||
* @param string $replace 替换内容
|
||||
* @return $this
|
||||
*/
|
||||
public function replace($content, $replace = '')
|
||||
{
|
||||
if (is_array($content)) {
|
||||
$this->replace = array_merge($this->replace, $content);
|
||||
} else {
|
||||
$this->replace[$content] = $replace;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user