找回密码
 立即注册

QQ登录

只需一步,快速开始

CodeIgniter 结合phpcms模板功能

crx349 于 2015-9-21 15:11 [CodeIgniter] 发表在 [复制链接] [显示全部楼层] [打印] [上一主题] [下一主题]
在CodeIgniter libraries中 增加 template_cache.php

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. *  模板解析缓存
  4. */
  5. final class template_cache {
  6.    
  7.     public $cache_path;
  8.     public function __construct()
  9.     {
  10.         //$CI =& get_instance();
  11.         $this->cache_path = APPPATH.'views';
  12.     }
  13.    
  14.     /**
  15.      * 编译模板
  16.      *
  17.      * @param $module    模块名称
  18.      * @param $template    模板文件名
  19.      * @param $istag    是否为标签模板
  20.      * @return unknown
  21.      */
  22.    
  23.     public function template_compile($module, $template, $style = 'default') {
  24.         
  25.         $tplfile= APPPATH.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
  26.         
  27.         if (! file_exists ( $tplfile )) {
  28.             show_error($tplfile ,  500 ,  'Template does not exist(1)');
  29.         }
  30.         
  31.         $content = @file_get_contents ( $tplfile );

  32.         $filepath = $this->cache_path.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
  33.         
  34.         
  35.         if(!is_dir($filepath)) {
  36.             mkdir($filepath, 0777, true);
  37.         }
  38.         $compiledtplfile = $filepath.$template.'.php';
  39.         $content = $this->template_parse($content);
  40.         $strlen = file_put_contents ( $compiledtplfile, $content );
  41.         chmod ( $compiledtplfile, 0777 );
  42.         return $strlen;
  43.     }
  44.    
  45.     /**
  46.      * 更新模板缓存
  47.      *
  48.      * @param $tplfile    模板原文件路径
  49.      * @param $compiledtplfile    编译完成后,写入文件名
  50.      * @return $strlen 长度
  51.      */
  52.     public function template_refresh($tplfile, $compiledtplfile) {
  53.         $str = @file_get_contents ($tplfile);
  54.         $str = $this->template_parse ($str);
  55.         $strlen = file_put_contents ($compiledtplfile, $str );
  56.         chmod ($compiledtplfile, 0777);
  57.         return $strlen;
  58.     }
  59.    

  60.     /**
  61.      * 解析模板
  62.      *
  63.      * @param $str    模板内容
  64.      * @return ture
  65.      */
  66.     public function template_parse($str) {
  67.         $str = preg_replace ( "/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $str );
  68.         $str = preg_replace ( "/\{include\s+(.+)\}/", "<?php include \\1; ?>", $str );
  69.         $str = preg_replace ( "/\{view\s+(.+)\}/", "<?php \$this->load->view(\\1); ?>", $str );
  70.         $str = preg_replace ( "/\{php\s+(.+)\}/", "<?php \\1?>", $str );
  71.         //alex fix
  72.         $str = preg_replace ( "/\{{if\s+(.+?)\}}/", "``if \\1``", $str );
  73.         $str = preg_replace ( "/\{{else\}}/", "``else``", $str );
  74.         $str = preg_replace ( "/\{{\/if\}}/", "``/if``", $str );
  75.         
  76.         $str = preg_replace ( "/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str );
  77.         $str = preg_replace ( "/\{else\}/", "<?php } else { ?>", $str );
  78.         $str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str );
  79.         $str = preg_replace ( "/\{\/if\}/", "<?php } ?>", $str );
  80.         
  81.         //for 循环
  82.         $str = preg_replace("/\{for\s+(.+?)\}/","<?php for(\\1) { ?>",$str);
  83.         $str = preg_replace("/\{\/for\}/","<?php } ?>",$str);
  84.         //++ --
  85.         $str = preg_replace("/\{\+\+(.+?)\}/","<?php ++\\1; ?>",$str);
  86.         $str = preg_replace("/\{\-\-(.+?)\}/","<?php ++\\1; ?>",$str);
  87.         $str = preg_replace("/\{(.+?)\+\+\}/","<?php \\1++; ?>",$str);
  88.         $str = preg_replace("/\{(.+?)\-\-\}/","<?php \\1--; ?>",$str);
  89.         //alex fix
  90.         $str = preg_replace ( "/\``if\s+(.+?)\``/", "{{if \\1}}", $str );
  91.         $str = preg_replace ( "/\``else``/", "{{else}}", $str );
  92.         $str = preg_replace ( "/\``\/if\``/", "{{/if}}", $str );
  93.         
  94.         $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $str );
  95.         $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $str );
  96.         $str = preg_replace ( "/\{\/loop\}/", "<?php \$n++;}unset(\$n); ?>", $str );
  97.         $str = preg_replace ( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
  98.         $str = preg_replace ( "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
  99.         $str = preg_replace ( "/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str );
  100.         $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'"\$\x7f-\xff]+)\}/es", "\$this->addquote('<?php echo \\1;?>')",$str);
  101.         $str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $str );
  102.         $str = preg_replace("/\{pc:(\w+)\s+([^}]+)\}/ie", "self::pc_tag('$1','$2', '$0')", $str);
  103.         $str = preg_replace("/\{\/pc\}/ie", "self::end_pc_tag()", $str);
  104.         $str = "<?php defined('BASEPATH') or exit('No direct script access allowed.'); ?>" . $str;
  105.         return $str;
  106.     }

  107.     /**
  108.      * 转义 // 为 /
  109.      *
  110.      * @param $var    转义的字符
  111.      * @return 转义后的字符
  112.      */
  113.     public function addquote($var) {
  114.         return str_replace ( "\\"", """, preg_replace ( "/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var ) );
  115.     }
  116.    
  117.     /**
  118.      * 解析PC标签
  119.      * @param string $op 操作方式
  120.      * @param string $data 参数
  121.      * @param string $html 匹配到的所有的HTML代码
  122.      */
  123.     public static function pc_tag($op, $data, $html) {
  124.         preg_match_all("/([a-z]+)\=["]?([^"]+)["]?/i", stripslashes($data), $matches, PREG_SET_ORDER);
  125.         $arr = array('action','num','cache','page', 'pagesize', 'urlrule', 'return', 'start','setpages');
  126.         $tools = array('json', 'xml', 'block', 'get');
  127.         $datas = array();
  128.         $tag_id = md5(stripslashes($html));
  129.         //可视化条件
  130.         $str_datas = 'op='.$op.'&tag_md5='.$tag_id;
  131.         foreach ($matches as $v) {
  132.             $str_datas .= $str_datas ? "&$v[1]=".($op == 'block' && strpos($v[2], '
  133. [/size][/font][/color][color=#111111][font=Verdana, Arial, Helvetica, sans-serif][size=13px]然后在global_helper中增加一个 template函数[/size][/font][/color]
  134. [color=#111111][font=Verdana, Arial, Helvetica, sans-serif][size=13px][code]if ( ! function_exists('template'))
  135. {
  136.     /**
  137.      * 模板调用
  138.      *
  139.      * @param $module
  140.      * @param $template
  141.      * @param $istag
  142.      * @return unknown_type
  143.      */
  144.     function template($module = 'expatree', $template = 'index', $style = 'expatree',$return_full_path=true) {
  145.         global $CI;
  146.         if(!isset($CI))$CI =& get_instance();
  147.         if(!$style) $style = 'default';
  148.         $CI->load->library('template_cache','template_cache');
  149.         $template_cache = $CI->template_cache;
  150.         //编译模板生成地址
  151.         $compiledtplfile = $template_cache->cache_path.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
  152.         //视图文件
  153.         $tplfile= APPPATH.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
  154.         if(file_exists($tplfile)) {
  155.             if(!file_exists($compiledtplfile) || (@filemtime($tplfile) > @filemtime($compiledtplfile))) {   
  156.                 $template_cache->template_compile($module, $template, $style);
  157.             }
  158.         } else {
  159.             //如果没有就调取默认风格模板
  160.             $compiledtplfile = $template_cache->cache_path.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
  161.             if(!file_exists($compiledtplfile) || (file_exists($tplfile) && filemtime($tplfile) > filemtime($compiledtplfile))) {
  162.                 $template_cache->template_compile($module, $template, 'default');
  163.             } elseif (!file_exists($tplfile)) {
  164.                 show_error($tplfile ,  500 ,  'Template does not exist(0)');
  165.             }
  166.         }

  167.         if($return_full_path)
  168.             return $compiledtplfile;
  169.         else
  170.             return 'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template;
  171.     }
  172. }
复制代码
然后在global_helper中增加一个 template函数
  1. if ( ! function_exists('template'))
  2. {
  3.     /**
  4.      * 模板调用
  5.      *
  6.      * @param $module
  7.      * @param $template
  8.      * @param $istag
  9.      * @return unknown_type
  10.      */
  11.     function template($module = 'expatree', $template = 'index', $style = 'expatree',$return_full_path=true) {
  12.         global $CI;
  13.         if(!isset($CI))$CI =& get_instance();
  14.         if(!$style) $style = 'default';
  15.         $CI->load->library('template_cache','template_cache');
  16.         $template_cache = $CI->template_cache;
  17.         //编译模板生成地址
  18.         $compiledtplfile = $template_cache->cache_path.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
  19.         //视图文件
  20.         $tplfile= APPPATH.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
  21.         if(file_exists($tplfile)) {
  22.             if(!file_exists($compiledtplfile) || (@filemtime($tplfile) > @filemtime($compiledtplfile))) {   
  23.                 $template_cache->template_compile($module, $template, $style);
  24.             }
  25.         } else {
  26.             //如果没有就调取默认风格模板
  27.             $compiledtplfile = $template_cache->cache_path.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
  28.             if(!file_exists($compiledtplfile) || (file_exists($tplfile) && filemtime($tplfile) > filemtime($compiledtplfile))) {
  29.                 $template_cache->template_compile($module, $template, 'default');
  30.             } elseif (!file_exists($tplfile)) {
  31.                 show_error($tplfile ,  500 ,  'Template does not exist(0)');
  32.             }
  33.         }

  34.         if($return_full_path)
  35.             return $compiledtplfile;
  36.         else
  37.             return 'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template;
  38.     }
  39. }
复制代码


然后在MY_Controller.php
增加一个方法
  1. /**
  2.      * 自动模板调用
  3.      *
  4.      * @param $module
  5.      * @param $template
  6.      * @param $istag
  7.      * @return unknown_type
  8.      */
  9.     protected function view($view_file,$page_data=false,$cache=false)
  10.     {
  11.         $view_file=$this->template($this->page_data['controller_name'].$this->page_data['module_name'],$view_file);
  12.         
  13.         $this->load->view($view_file,$page_data);
  14.     }
复制代码

转载: hubj.cnblogs.com

本教程由无限星辰工作室CRX349独家整理和提供,转载请注明地址,谢谢。本文地址:https://www.xmspace.net/thread-375-1-1.html
无限星辰工作室  好集导航 Discuz全集下载  星辰站长网  集热爱361  一品文学  手机小游戏合集   海外空间网 星辰api  星辰支付二维码管理平台 阿里云服务器 腾讯云服务器
服务Discuz!建站|DiscuzQ配置|二开|小程序|APP|搬家|挂马清理|防护|Win/Linux环境搭建|优化|运维|
服务理念:专业 诚信 友好QQ842062626 服务项目 Q群315524225

发表于 2015-9-21 15:11:08 | 显示全部楼层 |阅读模式

回复 | 使用道具 举报

该帖共收到 0 条回复!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

美图秀

    • fastadmin 后台界面使用字段数组类型
    • Discuz!x3.5 修改标题高亮颜色
    • Discuz!x3.5 应用中心 下载应用一直下载中
    • 帖子定时显示
    • 论坛辅助审核
拖动客服框
Online Service
点击这里给我发消息
点击这里联系我们
微信扫一扫
在线客服
快速回复 返回顶部 返回列表