zoukankan      html  css  js  c++  java
  • (4) PHP 随笔---Smarty模板引擎MiniSmarty及其优化 03-04

    ◇如何在类的内部使用外部声明的变量:

            ◇可以把模板引擎内部使用的变量信息,都声明为该模板类的属性信息,这样在累的内部可以正常使用本身的属性信息。

            ◇改进后的MiniSmarty.class.php的代码如下:

     1 <?php
     2     class MiniSmarty
     3     {
     4 
     5         public $tpl_var = array();
     6         function assign($key,$content){
     7             $this -> tpl_var[$key] = $content;
     8             
     9         }
    10         
    11         
    12         function compile($tpl){//花括号标记替换为PHP标记
    13             $cont = file_get_contents($tpl);
    14             //echo $cont;
    15 
    16             //替换  "{"  ---->     "<?php echo"
    17             $cont = str_replace("{$","<?php echo $this->tpl_var['",$cont);
    18             $cont = str_replace("}","']; ?>",$cont);
    19 
    20             //把生成好的编译内容(php+html混编内容)放入一个文件内
    21             file_put_contents('./tpl.html.php',$cont);
    22             require_once "tpl.html.php";
    23         }
    24 
    25     }
    26 ?>

        ◇而001.php的代码也需要做出改变,代码如下:

    <?php
       $name = "xixii" ;
       $age = 19 ;
       $height = 180 ;
    
       require "MiniSmarty.class.php";
       $smarty = new MiniSmarty();
                    //做出改变的代码在这里
       $smarty -> assign('name','xixi');
       $smarty -> assign('age','19');
       $smarty -> assign('height','180');
                    //做出改变的代码在这里
       $smarty -> compile('001.html');
       
    ?>    

    ◇上面的模板引擎已经可以使用了,但是我们还是可以进行缓存优化,减少服务器压力

          ◇混编文件已经存在,就不需要再反复生成,可以直接使用,可以利用一个判断来实现,其中判断条件有两个,需要注意。

     1 <?php
     2     class MiniSmarty
     3     {
     4 
     5         public $tpl_var = array();
     6         public $template_dir = "./view/";
     7         public $template_c_dir = "./view_c/";
     8         function assign($key,$content){
     9             $this -> tpl_var[$key] = $content;
    10             
    11         }
    12         
    13         function display($tpl){
    14             $n = $this -> compile($tpl);
    15             require_once $n;
    16         }
    17         
    18         function compile($tpl){//花括号标记替换为PHP标记
    19             $com_file = $this -> template_c_dir . $tpl.".php";
    20             $tpl_file = $this -> template_dir . $tpl;
    21             //走'已经生成'的混编文件需要满足下面两个条件
    22             //①来判断混编文件是否存在,
    23             //②还需要通过混编文件的时间,要大于模板文件的修改时间
    24             if(file_exists($com_file) && filemtime($com_file) > filemtime($tpl))
    25             {
    26                 return $com_file;
    27             }
    28             else
    29             {
    30                 echo "new file";
    31             $cont = file_get_contents($tpl);
    32             //echo $cont;
    33 
    34             //替换  "{"  ---->     "<?php echo"
    35             $cont = str_replace("{$","<?php echo $this->tpl_var['",$cont);
    36             $cont = str_replace("}","']; ?>",$cont);
    37 
    38             //把生成好的编译内容(php+html混编内容)放入一个文件内
    39             file_put_contents($com_file,$cont);
    40             return $com_file;
    41             }
    42         }
    43 
    44     }
    45 ?>
  • 相关阅读:
    [Leetcode] Merge Intervals
    [Leetcode] Sort Colors
    junit
    DBUnit的使用
    xml简介---来自百度百科
    今天开始深入学习XML
    Java 用Myeclipse部署项目基础坏境搭建
    properties配置文件读取方法
    Java web做服务器之间的通信方法
    Java Socket简单的客服端及其服务器端
  • 原文地址:https://www.cnblogs.com/kaolalovemiaomiao/p/4857163.html
Copyright © 2011-2022 走看看