• 技术文章 >后端开发 >php教程

    Pain 全世界最小最简单的PHP模板引擎 (普通版)_php模板_PHP

    2016-06-01 12:14:40原创322
    打包下载

    Pain.php
    复制代码 代码如下:
    class Pain
    {
    public $var=array();
    public $tpl=array();
    //this is the method to assign vars to the template
    public function assign($variable,$value=null)
    {
    $this->var[$variable]=$value;
    }
    public function display($template_name,$return_string=false)
    {
    //first find whether the tmp file in tmp dir exists.
    if(file_exists("tmp/temp_file.php"))
    {
    unlink("tmp/temp_file.php");
    }
    extract($this->var);
    $tpl_content=file_get_contents($template_name);
    $tpl_content=str_replace("{@", "$tpl_content=str_replace("@}", " ?>", $tpl_content);
    //create a file in the /tmp dir and put the $tpl_contentn into it, then
    //use 'include' method to load it!
    $tmp_file_name="temp_file.php";
    //$tmp is the handler
    $tmp=fopen("tmp/".$tmp_file_name, "w");
    fwrite($tmp, $tpl_content);
    include "tmp/".$tmp_file_name;
    }
    }
    ?>

    test.php
    复制代码 代码如下:
    require_once "Pain.php";
    $pain=new Pain();
    $songyu="songyu nb";
    $zhangyuan="zhangyuan sb";
    $pain->assign("songyu",$songyu);
    $pain->assign("zhangyuan",$zhangyuan);
    $pain->display("new_file.html");
    ?>

    new_file.html
    复制代码 代码如下:
    "http://www.w3.org/TR/html4/strict.dtd">



    new_file


    {@$songyu@}

    {@$zhangyuan@}

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:模板引擎
    上一篇:php数组函数序列之array_unique() 下一篇:使用PHP实现密保卡功能实现代码<打包下载直接运行&gt_PHP
    PHP编程就业班

    相关文章推荐

    • 浅析PHP配置文件中的几种超时配置• 聊聊nginx平滑重启和FPM平滑重启• 归纳总结PHP对象基础• 请问疑难杂症 • 求这个网站的基本词挖掘功能的做法,求实现代码

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网