Home > Backend Development > PHP Tutorial > How CodeIgniter uses the view template engine of phpcms

How CodeIgniter uses the view template engine of phpcms

不言
Release: 2023-03-22 16:04:01
Original
1538 people have browsed it

本文和大家分享了CodeIgniter如何使用phpcms的view模板解析功能,希望对大家有所帮助。

CodeIgniter很适合小站点应用开发,但是它自带的view功能可能会给不懂PHP的前端人员带来麻烦。 相比之下phpcms的view模板解析就强大多了,所以这里就把PHPCMS的模板解析功能剥离出来,加到PHPCMS上。
首先在CodeIgniter libraries中 增加 template_cache.php

代码如下:

<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;); 
/**
 *  模板解析缓存
 */
final class template_cache {

    public $cache_path;
    public function __construct()
    {
        //$CI =& get_instance();
        $this->cache_path = APPPATH.&#39;views&#39;;
    }

    /**
     * 编译模板
     *
     * @param $module    模块名称
     * @param $template    模板文件名
     * @param $istag    是否为标签模板
     * @return unknown
     */

    public function template_compile($module, $template, $style = &#39;default&#39;) {

        $tplfile= APPPATH.&#39;views&#39;.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.&#39;.php&#39;;

        if (! file_exists ( $tplfile )) {
            show_error($tplfile ,  500 ,  &#39;Template does not exist(1)&#39;);
        }

        $content = @file_get_contents ( $tplfile );
        $filepath = $this->cache_path.DIRECTORY_SEPARATOR.&#39;caches_template&#39;.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;

        
        if(!is_dir($filepath)) {
            mkdir($filepath, 0777, true);
        }
        $compiledtplfile = $filepath.$template.&#39;.php&#39;;
        $content = $this->template_parse($content);
        $strlen = file_put_contents ( $compiledtplfile, $content );
        chmod ( $compiledtplfile, 0777 );
        return $strlen;
    }

    /**
     * 更新模板缓存
     *
     * @param $tplfile    模板原文件路径
     * @param $compiledtplfile    编译完成后,写入文件名
     * @return $strlen 长度
     */
    public function template_refresh($tplfile, $compiledtplfile) {
        $str = @file_get_contents ($tplfile);
        $str = $this->template_parse ($str);
        $strlen = file_put_contents ($compiledtplfile, $str );
        chmod ($compiledtplfile, 0777);
        return $strlen;
    }
   
    /**
     * 解析模板
     *
     * @param $str    模板内容
     * @return ture
     */
    public function template_parse($str) {
        $str = preg_replace ( "/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $str );
        $str = preg_replace ( "/\{include\s+(.+)\}/", "<?php include \\1; ?>", $str );
        $str = preg_replace ( "/\{view\s+(.+)\}/", "<?php \$this->load->view(\\1); ?>", $str );
        $str = preg_replace ( "/\{php\s+(.+)\}/", "<?php \\1?>", $str );
        //alex fix
        $str = preg_replace ( "/\{{if\s+(.+?)\}}/", "``if \\1``", $str );
        $str = preg_replace ( "/\{{else\}}/", "``else``", $str );
        $str = preg_replace ( "/\{{\/if\}}/", "``/if``", $str );

        $str = preg_replace ( "/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str );
        $str = preg_replace ( "/\{else\}/", "<?php } else { ?>", $str );
        $str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str );
        $str = preg_replace ( "/\{\/if\}/", "<?php } ?>", $str );

        //for 循环
        $str = preg_replace("/\{for\s+(.+?)\}/","<?php for(\\1) { ?>",$str);
        $str = preg_replace("/\{\/for\}/","<?php } ?>",$str);
        //++ --
        $str = preg_replace("/\{\+\+(.+?)\}/","<?php ++\\1; ?>",$str);
        $str = preg_replace("/\{\-\-(.+?)\}/","<?php ++\\1; ?>",$str);
        $str = preg_replace("/\{(.+?)\+\+\}/","<?php \\1++; ?>",$str);
        $str = preg_replace("/\{(.+?)\-\-\}/","<?php \\1--; ?>",$str);
        //alex fix
        $str = preg_replace ( "/\``if\s+(.+?)\``/", "{{if \\1}}", $str );
        $str = preg_replace ( "/\``else``/", "{{else}}", $str );
        $str = preg_replace ( "/\``\/if\``/", "{{/if}}", $str );

        $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $str );
        $str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $str );
        $str = preg_replace ( "/\{\/loop\}/", "<?php \$n++;}unset(\$n); ?>", $str );
        $str = preg_replace ( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
        $str = preg_replace ( "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
        $str = preg_replace ( "/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str );
        $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\&#39;\"\$\x7f-\xff]+)\}/es", "\$this->addquote(&#39;<?php echo \\1;?>&#39;)",$str);
        $str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $str );
        $str = preg_replace("/\{pc:(\w+)\s+([^}]+)\}/ie", "self::pc_tag(&#39;$1&#39;,&#39;$2&#39;, &#39;$0&#39;)", $str);
        $str = preg_replace("/\{\/pc\}/ie", "self::end_pc_tag()", $str);
        $str = "<?php defined(&#39;BASEPATH&#39;) or exit(&#39;No direct script access allowed.&#39;); ?>" . $str;
        return $str;
    }
    /**
     * 转义 // 为 /
     *
     * @param $var    转义的字符
     * @return 转义后的字符
     */
    public function addquote($var) {
        return str_replace ( "\\\"", "\"", preg_replace ( "/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "[&#39;\\1&#39;]", $var ) );
    }

    /**
     * 解析PC标签
     * @param string $op 操作方式
     * @param string $data 参数
     * @param string $html 匹配到的所有的HTML代码
     */
    public static function pc_tag($op, $data, $html) {
        preg_match_all("/([a-z]+)\=[\"]?([^\"]+)[\"]?/i", stripslashes($data), $matches, PREG_SET_ORDER);
        $arr = array(&#39;action&#39;,&#39;num&#39;,&#39;cache&#39;,&#39;page&#39;, &#39;pagesize&#39;, &#39;urlrule&#39;, &#39;return&#39;, &#39;start&#39;,&#39;setpages&#39;);
        $tools = array(&#39;json&#39;, &#39;xml&#39;, &#39;block&#39;, &#39;get&#39;);
        $datas = array();
        $tag_id = md5(stripslashes($html));
        //可视化条件
        $str_datas = &#39;op=&#39;.$op.&#39;&tag_md5=&#39;.$tag_id;
        foreach ($matches as $v) {
            $str_datas .= $str_datas ? "&$v[1]=".($op == &#39;block&#39; && strpos($v[2], &#39;$&#39;) === 0 ? $v[2] : urlencode($v[2])) : "$v[1]=".(strpos($v[2], &#39;$&#39;) === 0 ? $v[2] : urlencode($v[2]));
            if(in_array($v[1], $arr)) {
                $$v[1] = $v[2];
                continue;
            }
            $datas[$v[1]] = $v[2];
        }
        $str = &#39;&#39;;
        $setpages = isset($setpages) && intval($setpages) ? intval($setpages) : 10;
        $num = isset($num) && intval($num) ? intval($num) : 20;
        $cache = isset($cache) && intval($cache) ? intval($cache) : 0;
        $return = isset($return) && trim($return) ? trim($return) : &#39;data&#39;;
        if (!isset($urlrule)) $urlrule = &#39;&#39;;
        if (!empty($cache) && !isset($page)) {
            $str .= &#39;$tag_cache_name = md5(implode(\&#39;&\&#39;,&#39;.self::arr_to_html($datas).&#39;).\&#39;&#39;.$tag_id.&#39;\&#39;);if(!$&#39;.$return.&#39; = tpl_cache($tag_cache_name,&#39;.$cache.&#39;)){&#39;;
        }
        if (in_array($op,$tools)) {
            switch ($op) {
                case &#39;json&#39;:
                        if (isset($datas[&#39;url&#39;]) && !empty($datas[&#39;url&#39;])) {
                            $str .= &#39;$json = @file_get_contents(\&#39;&#39;.$datas[&#39;url&#39;].&#39;\&#39;);&#39;;
                            $str .= &#39;$&#39;.$return.&#39; = json_decode($json, true);&#39;;
                        }
                    break;

                case &#39;block&#39;:
                    $str .= &#39;$block_tag = pc_base::load_app_class(\&#39;block_tag\&#39;, \&#39;block\&#39;);&#39;;
                    $str .= &#39;echo $block_tag->pc_tag(&#39;.self::arr_to_html($datas).&#39;);&#39;;
                    break;
            }
        } else {
            if (!isset($action) || empty($action)) return false;
            if ( file_exists(APPPATH.&#39;libraries&#39;.DIRECTORY_SEPARATOR.$op.&#39;_tag.php&#39;)) {
                $str .= &#39;if(!isset($CI))$CI =& get_instance();$CI->load->library("&#39;.$op.&#39;_tag");if (method_exists($CI->&#39;.$op.&#39;_tag, \&#39;&#39;.$action.&#39;\&#39;)) {&#39;;    
                if (isset($start) && intval($start)) {
                    $datas[&#39;limit&#39;] = intval($start).&#39;,&#39;.$num;
                } else {
                    $datas[&#39;limit&#39;] = $num;
                }
                if (isset($page)) {
                    $str .= &#39;$pagesize = &#39;.$num.&#39;;&#39;;
                    $str .= &#39;$page = intval(&#39;.$page.&#39;) ? intval(&#39;.$page.&#39;) : 1;if($page<=0){$page=1;}&#39;;
                    $str .= &#39;$offset = ($page - 1) * $pagesize;$urlrule="&#39;.$urlrule.&#39;";&#39;;
                    $datas[&#39;limit&#39;] = &#39;$offset.",".$pagesize&#39;;
                    $datas[&#39;action&#39;] = $action;
                    $str .= &#39;$&#39;.$op.&#39;_total = $CI->&#39;.$op.&#39;_tag->count(&#39;.self::arr_to_html($datas).&#39;);&#39;;

                    $str .= &#39;if($&#39;.$op.&#39;_total>$pagesize){ $pages = pages($&#39;.$op.&#39;_total, $page, $pagesize, $urlrule); } else { $pages="" ;}&#39;;
                }
                $str .= &#39;$&#39;.$return.&#39; = $CI->&#39;.$op.&#39;_tag->&#39;.$action.&#39;(&#39;.self::arr_to_html($datas).&#39;);&#39;;
                $str .= &#39;}&#39;;
            } 
        }
        if (!empty($cache) && !isset($page)) {
            $str .= &#39;if(!empty($&#39;.$return.&#39;)){setcache($tag_cache_name, $&#39;.$return.&#39;, \&#39;tpl_data\&#39;);}&#39;;
            $str .= &#39;}&#39;;
        }
        return "<"."?php ".$str."?".">";
    }

    /**
     * PC标签结束
     */
    static private function end_pc_tag() {
        return &#39;<?php if(defined(\&#39;IN_ADMIN\&#39;) && !defined(\&#39;HTML\&#39;)) {if(isset($data))unset($data);echo \&#39;</p>\&#39;;}?>&#39;;
    }

    /**
     * 转换数据为HTML代码
     * @param array $data 数组
     */
    private static function arr_to_html($data) {
        if (is_array($data)) {
            $str = &#39;array(&#39;;
            foreach ($data as $key=>$val) {
                if (is_array($val)) {
                    $str .= "&#39;$key&#39;=>".self::arr_to_html($val).",";
                } else {
                    if (strpos($val, &#39;$&#39;)===0) {
                        $str .= "&#39;$key&#39;=>$val,";
                    } else {
                        $str .= "&#39;$key&#39;=>&#39;".self::new_addslashes($val)."&#39;,";
                    }
                }
            }
            return $str.&#39;)&#39;;
        }
        return false;
    }

    /**
     * 返回经addslashes处理过的字符串或数组
     * @param $string 需要处理的字符串或数组
     * @return mixed
     */
    function new_addslashes($string){
        if(!is_array($string)) return addslashes($string);
        foreach($string as $key => $val) $string[$key] = new_addslashes($val);
        return $string;
    }
}
Copy after login

然后在global_helper中增加一个 template函数

代码如下:

if ( ! function_exists(&#39;template&#39;))
{
    /**
     * 模板调用
     * 
     * @param $module
     * @param $template
     * @param $istag
     * @return unknown_type
     */
    function template($module = &#39;expatree&#39;, $template = &#39;index&#39;, $style = &#39;expatree&#39;,$return_full_path=true) {
        global $CI;
        if(!isset($CI))$CI =& get_instance();
        if(!$style) $style = &#39;default&#39;;
        $CI->load->library(&#39;template_cache&#39;,&#39;template_cache&#39;);
        $template_cache = $CI->template_cache;
        //编译模板生成地址
        $compiledtplfile = $template_cache->cache_path.DIRECTORY_SEPARATOR.&#39;caches_template&#39;.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
        //视图文件
        $tplfile= APPPATH.&#39;views&#39;.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
        if(file_exists($tplfile)) {
            if(!file_exists($compiledtplfile) || (@filemtime($tplfile) > @filemtime($compiledtplfile))) {    
                $template_cache->template_compile($module, $template, $style);
            }
        } else {
            //如果没有就调取默认风格模板
            $compiledtplfile = $template_cache->cache_path.DIRECTORY_SEPARATOR.&#39;caches_template&#39;.DIRECTORY_SEPARATOR.&#39;default&#39;.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.EXT;
            if(!file_exists($compiledtplfile) || (file_exists($tplfile) && filemtime($tplfile) > filemtime($compiledtplfile))) {
                $template_cache->template_compile($module, $template, &#39;default&#39;);
            } elseif (!file_exists($tplfile)) {
                show_error($tplfile ,  500 ,  &#39;Template does not exist(0)&#39;);
            }
        }
        if($return_full_path)
            return $compiledtplfile;
        else
            return &#39;caches_template&#39;.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template;
    }
}
Copy after login

然后在MY_Controller.php,增加一个方法

代码如下:

/**
    * 自动模板调用
    * 
    * @param $module
    * @param $template
    * @param $istag
    * @return unknown_type
    */
   protected function view($view_file,$page_data=false,$cache=false)
   {
       $view_file=$this->template($this->page_data[&#39;controller_name&#39;].$this->page_data[&#39;module_name&#39;],$view_file);

       $this->load->view($view_file,$page_data);
   }
Copy after login

这样基本上完成了,可以直接phpcms模板语法了。

相关推荐:

php Codeigniter实现智能裁剪图片的方法

phpcms配置列表页实例详解



The above is the detailed content of How CodeIgniter uses the view template engine of phpcms. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template