zoukankan      html  css  js  c++  java
  • 简单模板类

    MyMiniSmarty.class.php

    View Code
     1 <?php
     2     /**
     3     * 模板类
     4     * 1、读取模板文件
     5     * 2、把模板文件替换成可以运行的php文件
     6     * 3、包含缓存机制--文件
     7     */
     8     
     9     class MyMiniSmarty {
    10         //var 为了兼容低版本
    11         var $template_dir = './templates/'; //模板文件
    12         var $compile_dir = './templates_c/'; //编译后文件
    13         var $tpl_vars = array(); //存放模板值的数组
    14         
    15         //给模板值
    16         function assign($tpl_var, $var = null) {
    17             if ('' != $tpl_var) {
    18                 $this->tpl_vars[$tpl_var] = $var;
    19             }
    20         }
    21         //替换占位符编译成可运行文件
    22         function display($tpl_file) {
    23             $tpl_file_path = $this->template_dir . $tpl_file;
    24             $complie_file_path = $this->compile_dir . 'com_' . $tpl_file . '.php';
    25             if (!file_exists($tpl_file_path)) {
    26                 return false;
    27             }
    28             //判断是否有缓存文件,以前编译过,检查模板文件和编译文件修改时间
    29             if (!file_exists($complie_file_path) || filemtime($tpl_file_path) > filemtime($complie_file_path)) {
    30                 $fpl_file_con = file_get_contents($tpl_file_path);
    31             
    32                 //核心是正则替换
    33                 
    34                 $pattern = array(
    35                     '/\{\s*\$([a-zA-Z_]\w*)\s*\}/i'
    36                 );
    37                 $replace = array(
    38                     '<?php echo $this->tpl_vars["${1}"] ?>'    
    39                 );
    40                 
    41                 $new_str = preg_replace($pattern, $replace, $fpl_file_con);
    42                 
    43                 file_put_contents($complie_file_path, $new_str);
    44             }
    45             
    46             
    47             
    48             
    49             //引用编译后的文件
    50             include($complie_file_path);
    51         }
    52         
    53     }
    54 ?>

    intro.php

    View Code
    <?php
        require 'MyMiniSmarty.class.php';
        $mysmarty = new MyMiniSmarty;
        
        $mysmarty->assign('title', '模板标题');
        $mysmarty->assign('content', '模板内容');
        $mysmarty->display('intro.tpl');
    ?>

    intro.tpl

    View Code
    <html>
    <head>
    <meta content="text/html" charset="utf-8"/>
    <title>{$title}</title>
    </head>
    <body>
    {$content}
    </body>
    </html>

    模板机制在于,简单标签代替php代码,使逻辑代码和页面设计分离,同时加入文件缓存等机制

  • 相关阅读:
    Moodle 3.8 安装过程
    Discourse 数据分发
    Moodle 安装的时候提示 original IP
    Apache 安装运行测时候提示错误 (13)Permission denied: AH00091: httpd: could not open error log file
    Apache 配置 SELinux 命令的时候的命令 semanage
    Apache Httpd 安装 AH00558错误
    Discourse 如何修改用户自己的密码
    Discourse 如何限制注册用户的密码长度
    Discourse 的快捷键列表
    S3 存储附件和图片无法上传
  • 原文地址:https://www.cnblogs.com/caps/p/2934217.html
Copyright © 2011-2022 走看看