您的位置:首页 > 编程语言 > PHP开发

ThinkPHP5.0.7支持手机电脑双模板的修改

2017-05-09 19:22 218 查看
1.创建一个view类继承系统的view类,建议位置(\application\common\controller\View.php)代码如下

<?php
namespace app\common\controller;
use think\view as DefauleView;
use think\Hook;

class View extends DefauleView{

/**
* 解析和获取模板内容 用于输出
* @param string    $template 模板文件名或者内容
* @param array     $vars     模板输出变量
* @param array     $replace 替换内容
* @param array     $config     模板参数
* @param bool      $renderContent     是否渲染内容
* @return string
* @throws Exception
*
* 修改使之能加载手机模板
*
*/
public function fetch($template = '', $vars = [], $replace = [], $config = [], $renderContent = false)
{
$request = \think\Request::instance();
if (!$template && $request->isMobile()){
$view = 'view-mobile';
$template_mobile = strtolower(APP_PATH.request()->module().DS.$view.DS.$request->controller().DS.$request->action().'.html');
if (is_file($template_mobile)){
$template = $template_mobile;
}
}
// 模板变量
$vars = array_merge(self::$var, $this->data, $vars);
// 页面缓存
ob_start();
ob_implicit_flush(0);

// 渲染输出
$method = $renderContent ? 'display' : 'fetch';
$this->engine->$method($template, $vars, $config);

// 获取并清空缓存
$content = ob_get_clean();
// 内容过滤标签
Hook::listen('view_filter', $content);
// 允许用户自定义模板的字符串替换
$replace = array_merge($this->replace, $replace);
if (!empty($replace)) {
$content = strtr($content, $replace);
}
return $content;
}
}


2.修改系统的Template.php文件,位置\thinkphp\library\think\Template.php

1)修改fetch方法 line186

public function fetch($template, $vars = [], $config = [])
{
if ($vars) {
$this->data = $vars;
}
if ($config) {
$this->config($config);
}
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
// 读取渲染缓存
$cacheContent = Cache::get($this->config['cache_id']);
if (false !== $cacheContent) {
echo $cacheContent;
return;
}
}
$template = $this->parseTemplateFile($template);
if ($template) {
/*
修改$cacheFile,系统默认是一个页面一个缓存,支持手机后,需要分电脑和手机单独缓存页面
*/
$cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template.Request::instance()->isMobile()) . '.' . ltrim($this->config['cache_suffix'], '.');
if (!$this->checkCache($cacheFile)) {
// 缓存无效 重新模板编译
$content = file_get_contents($template);
$this->compiler($content, $cacheFile);
}
// 页面缓存
ob_start();
ob_implicit_flush(0);
// 读取编译存储
$this->storage->read($cacheFile, $this->data);
// 获取并清空缓存
$content =
4000
ob_get_clean();
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
// 缓存页面输出
Cache::set($this->config['cache_id'], $content, $this->config['cache_time']);
}
echo $content;
}
}


2)修改parseTemplateFile方法 line1077 1080

private function parseTemplateFile($template)
{
if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
if (strpos($template, '@')) {
list($module, $template) = explode('@', $template);
}
if (0 !== strpos($template, '/')) {
$template = str_replace(['/', ':'], $this->config['view_depr'], $template);
} else {
$template = str_replace(['/', ':'], $this->config['view_depr'], substr($template, 1));
}
if ($this->config['view_base']) {
$module = isset($module) ? $module : Request::instance()->module();
$path   = $this->config['view_base'] . ($module ? $module . DS : '');
} else {
$path = isset($module) ? APP_PATH . $module . DS . basename($this->config['view_path']) . DS : $this->config['view_path'];
}

$template_tmp = $template;//添加的
$template = $path . $template . '.' . ltrim($this->config['view_suffix'], '.');
}

//对于手机模板处理 添加的
if (Request::instance()->isMobile()){
$template_mobile = str_replace('view','view-mobile',$path) . $template_tmp . '.' . ltrim($this->config['view_suffix'], '.');
if (is_file($template_mobile)){
$template = $template_mobile;
}
}

if (is_file($template)) {
// 记录模板文件的更新时间
$this->includeFile[$template] = filemtime($template);
return $template;
} else {
throw new TemplateNotFoundException('template not exists:' . $template, $template);
}
}


3.使用view类

$view = new \app\common\controller\View();
$this->view = $view;
return $this->view->fetch();

前台html文件中,文件包含用
{include file="public/header" /}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp
相关文章推荐