栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

自定义简单的PHP模版引擎

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义简单的PHP模版引擎

定义文件:

    1. 创建目录文件tpl

    2. 创建模版处理文件tpl/Template.php

    3. 显示处理页面 tpl/index.php

    4. 创建模版文件 tpl/index.html

    5. 编译目录文件 tpl/compile


tpl/Template.php源代码

tplVals as $k=>$v) {
            $data = str_replace('{$'.$k.'}', $v, $data);
        }
        return $data;
    }
 
    
    public function display($tpl) {
        // 获取模版内容
        $content = file_get_contents($tpl.$this->tplExtendName);
         
        // 替换模版中的变量
        $content = $this->replaceTplVar($content);
 
 
        // 编译后的文件
        $compileFile = $this->compileFile.md5($tpl).$this->compileExtendName;
         
        // 给编译后的文件添加内容
        file_put_contents($compileFile, $content);
         
        // 引入编译文件
        require_once $compileFile;
         
    }
 
 
    
    public function assign($name, $value) {
        $this->tplVals[$name] = $value;
    }
}


tpl/index.php源代码

assign('title','自定义smart有模版引擎');
$tpl->assign('content','这是模本内容');
$tpl->display('index');

 tpl/index.html源代码




欢迎大家来零壹码学习自定义模版引擎
   


    {$title}
    

{$content}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228855.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号