zoukankan      html  css  js  c++  java
  • 如何通过smarty生成静态化网页

    Smarty最大的功能是做模版的页面缓存。也就是通过Smarty可以完成两个步骤:编译+解析

    第一步:编译。是指把模版文件的标签替换为纯php,再保存在缓存位置,保存的文件扩展名是PHP,我把这个步骤叫做编译(这是我自己的叫法,不是官方的)

    第二步:解析。也就是把刚才编译的PHP文件解析执行而已~~这个就不用多做解释了

    切入正题,在Smarty.class.php文件中加入如下代码

    function MakeHtmlFile($file_name, $content)
    {     
    //目录不存在就创建 
         
    if (!file_exists (dirname($file_name
    ))) { 
              if (!@
    mkdir (dirname($file_name), 0777
    )) { 
                  die(
    $file_name."目录创建失败!"
    ); 
              } 
          } 
                         
              if(!
    $fp = fopen($file_name, "w"
    )){
                  echo
    "文件打开失败!"
    ;
                  return
    false
    ;
              } 

              if(!
    fwrite($fp, $content
    )){
                  echo
    "文件写入失败!"
    ;
                 
    fclose($fp
    );
                  return
    false
    ;
              }
             
             
    fclose($fp
    );
                 
    chmod($file_name,0666
    );
          }
    这个函数的作用就是保存文件~~

    调用方法如下

    require '../libs/Smarty.class.php';
    $smarty = new Smarty
    ;
    //…………省略变量定义和赋值
    //$smarty->display('index.tpl');
    $content=$smarty->fetch("index.tpl"
    );

    $smarty->MakeHtmlFile('./index.html',$content);//生成

  • 相关阅读:
    Cookie和Session的区别
    get和post的区别
    TCP和UDP的区别
    TCP三次握手过程
    docker 安装prometheus
    大数据集群环境搭建之一 hadoop-ha高可用安装
    大数据集群环境 zookeeper集群环境安装
    Centos 脚本中几个特殊符号的作用笔记
    VMware 设置虚拟机Centos 上网的两种方式
    大数据集群环境搭建之一 Centos基本环境准备
  • 原文地址:https://www.cnblogs.com/kuyuecs/p/1594495.html
Copyright © 2011-2022 走看看