ThinkPHP5.0.7支持手机电脑双模板的修改
2017-05-09 19:22
218 查看
1.创建一个view类继承系统的view类,建议位置(\application\common\controller\View.php)代码如下
2.修改系统的Template.php文件,位置\thinkphp\library\think\Template.php
1)修改fetch方法 line186
2)修改parseTemplateFile方法 line1077 1080
3.使用view类
<?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" /}
相关文章推荐
- pycharm修改代码模板支持中文输出
- 使用基于ThinkPHP3.2.3的ThinkAdmin创建手机电脑通用的表白墙(二)自动判断设备以显示不同界面
- 使用基于ThinkPHP3.2.3的ThinkAdmin创建手机电脑通用的表白墙(七)优化:访问控制
- 关于安卓(Android)手机,安卓(Android)平板电脑,是否支持USB主机(USB Host)功能
- 手机模板修改
- ecshop如何不用做wap模板,手机访问和电脑访问界面一致
- andorid程序同时支持手机 平板电脑
- 织梦模板修改{dede:list}标签后,可支持权重排序、flag标签功能
- Android 使用模板生成Word文档,支持手机直接查看word
- 如何修改android手机MTP,PTP,充电模式下 在windows电脑任务栏显示的"弹出Android","弹出MTP"提示语
- 关于 local host 修改 如何修改电脑上的host 同时映射到手机上
- 使用基于ThinkPHP3.2.3的ThinkAdmin创建手机电脑通用的表白墙(四)实现弹幕
- 触控鼠标 (使用手机控制电脑,支持IOS,android.OSx 和windows)进入初步推广阶段
- 【转】phpcms 手机版本m. 电脑版www. 双模板操作
- 支持win8的平板电脑和手机
- Thinkphp修改默认模板引擎为smarty如何操作
- Thinkphp5.0完全相同的域名下访问电脑和手机网站
- phpcms v9电脑pc站+手机wap移动端双模板共用数据库数据同步可同步生成静态文件
- flexBuilder3中生成的模板页不支持flash全屏的修改办法
- 转载--thinkphp模板中支持php的if else 判断标签