本文实例讲述了thinkPHP3.0框架实现模板保存到数据库的方法。分享给大家供大家参考,具体如下:
在开发cms的时候用到如果将模板文件存入到数据库并显示到页面中
由于thinkphp3.0都是直接从模板文件中读取再解析的那么对于模板存入数据库中就只有自己开发了,还有thinkphp3.0中有mode的功能我们可以定义自己的mode这样就可以达到目的了,那么如何来扩展自己的mode呢?如下:
1.在你的入口文件中输入
define('MODE_NAME','Ey');
其中"Ey"就是你自己扩展的mode名称了,请在你的thinkphp/Extend/Mode文件下面创建Ey文件夹
2.在Ey目录中修改
添加tags.php文件内容如下:
return array(
'app_init'=>array(
),
'app_begin'=>array(
'ReadHtmlCache', // 读取静态缓存
),
'route_check'=>array(
'CheckRoute', // 路由检测
),
'app_end'=>array(),
'path_info'=>array(),
'action_begin'=>array(),
'action_end'=>array(),
'view_begin'=>array(),
'view_template'=>array(
'ExtensionTemplate', // 自动定位模板文件(手动添加)
),
'view_content'=>array(
'ParseContent'//(手动添加)
),
'view_filter'=>array(
'ContentReplace', // 模板输出替换
'TokenBuild', // 表单令牌
'WriteHtmlCache', // 写入静态缓存
'ShowRuntime', // 运行时间显示
),
'view_end'=>array(
'ShowPageTrace', // 页面Trace显示
),
);
该文件中后面的注释中添加手动添加了为我的修改,只是修改thinkphp中默认的tags中查找模板和解析模板的行为
将系统默认的action和view类复制到Ey的目录中(由于解析内容,所以要修改action和view类),修改action.class.php中的fetch方法:
protected function fetch($templateFile='',$templateContent='' ){
return $this->view->fetch($templateFile,$templateContent);
}
view.class.php文件中的修改为:
public function fetch($templateFile='',$templateContent = NULL) {
$params['templateFile'] = $templateFile;
$params['cacheFlag'] = true;
if(isset($templateContent)) {
$params['templateContent'] = $templateContent;
}
tag('view_template',$params);
// 页面缓存
ob_start();
ob_implicit_flush(0);
if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板
// 模板阵列变量分解成为独立变量
extract($this->tVar, EXTR_OVERWRITE);
// 直接载入PHP模板
include $templateFile;
}else{
// 视图解析标签
$params = array('var'=>$this->tVar,'content'=>$params['templateContent'],'file'=>$params['templateFile'],'cacheFlag'=>$params['cacheFlag']);
tag('view_content',$params);
}
// 获取并清空缓存
$content = ob_get_clean();
// 内容过滤标签
tag('view_filter',$content);
// 输出模板文件
return $content;
}
3.扩展自己的查找模板的类(自己扩展的行为tp让我们放在thinkphp\Extend\Behavior中)
在thinkphp\Extend\Behavior中添加ExtensionTemplateBehavior.class.php类,内容如下:
class ExtensionTemplateBehavior extends Behavior {
// 行为扩展的执行入口必须是run
public function run(&$params){
if( is_array($params) ){
if( array_key_exists('templateFile', $params) ){
$params = $this->parseTemplateFile($params);
}else{
//异常
throw_exception(L('_TEMPLATE_NOT_EXIST_AND_CONTENT_NULL_').'['.$params['templateFile'].']');
}
}else{
// 自动定位模板文件
if(!file_exists_case($params))
$params = $this->parseTemplateFile($params);
}
}
private function parseTemplateFile($params) {
if( is_array($params) ) {
$templateFile = $params['templateFile'];
}else{
$templateFile = $params;
}
if(!isset($params['templateContent'])) { // 是否设置 templateContent 参数
//自动获取模板文件
if('' == $templateFile){
// 如果模板文件名为空 按照默认规则定位
$templateFile = C('TEMPLATE_NAME');
} elseif(false === strpos($templateFile,C('TMPL_TEMPLATE_SUFFIX'))) {
$path = explode(':',$templateFile);
//如果是插件
if($path[0] == 'Ext') {
$templateFile = str_replace(array('Ext:',$path[1] . ':',$path[2] . ':'),'',$templateFile);
$templateFile = SITE_ROOT . '/Ext/extensions/' . strtolower($path[1]) . '/' . $path[2] . '/Tpl/' . $templateFile . C('TMPL_TEMPLATE_SUFFIX');
} else {
// 解析规则为 模板主题:模块:操作 不支持 跨项目和跨分组调用
$action = array_pop($path);
$module = !empty($path)?array_pop($path):MODULE_NAME;
if(!empty($path)) {// 设置模板主题
$path = dirname(THEME_PATH).'/'.array_pop($path).'/';
}else{
$path = THEME_PATH;
}
$depr = defined('GROUP_NAME')?C('TMPL_FILE_DEPR'):'/';
$templateFile = $path.$module.$depr.$action.C('TMPL_TEMPLATE_SUFFIX');
}
}
} else {
if('' == $templateFile){
$depr = defined('GROUP_NAME')?C('TMPL_FILE_DEPR'):'/';
$params['cacheFlag'] = false;
} else {
$path = explode(':',$templateFile);
//如果是插件
if($path[0] == 'Ext') {
$templateFile = str_replace(array('Ext:',$path[1] . ':',$path[2] . ':'),'',$templateFile);
$templateFile = SITE_ROOT . '/Ext/extensions/' . strtolower($path[1]) . '/' . $path[2] . '/Tpl/' . $templateFile . C('TMPL_TEMPLATE_SUFFIX');
} else {
// 解析规则为 模板主题:模块:操作 不支持 跨项目和跨分组调用
$action = array_pop($path);
$module = !empty($path)?array_pop($path):MODULE_NAME;
if(!empty($path)) {// 设置模板主题
$path = dirname(THEME_PATH).'/'.array_pop($path).'/';
}else{
$path = THEME_PATH;
}
$depr = defined('GROUP_NAME')?C('TMPL_FILE_DEPR'):'/';
$templateFile = $path.$module.$depr.$action.C('TMPL_TEMPLATE_SUFFIX');
}
}
}
if( is_array($params) ){
$params['templateFile'] = $templateFile;
return $params;
}else{
if(!file_exists_case($templateFile))
throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
return $templateFile;
}
}
}
pbootcms模板自动生成当前页面二维码二维码生成标签 {pboot:qrcode string=***} 使用说明: 用于生成对应文本的二维码图片,可用于产品列表页或详情页为每个
pbootcms模板中那些url怎么调用1、当前站点网址: {pboot:httpurl} 使用说明: 自适应获取当前访问网址,主要用于需要使用网站路径前缀的情况,如输
PbootCMS设置当前站点模板,模板子目录,黑白名单,敏感词过滤后台操作更换模板路径: 【基础内容】-【站点信息】-【站点模板】 配置后台模板子目录 【全局配置】-【配置参数
pbootcms公共标签调用1、模板文件嵌套引用 {include file=***.html} 使用说明: 文章来源:html5模板网 html5code.net 来源:html5模板网 html5code.net 可
PHP 使用二进制保存用户状态的实例下面小编就为大家分享一篇PHP 使用二进制保存用户状态的实例,具有很好的参考价值,希望对大家有所帮助。一起跟
Laravel框架之blade模板新手入门教程及小技巧Blade 是 laravel 提供的一个简单强大的模板引擎。下面这篇文章主要给大家介绍了关于Laravel框架之blade模板新手的入门