首页 > php教程 > PHP源码 > 正文

读取、把模板中的标签解析成PHP语法编译成PHP文件

PHP中文网
发布: 2016-05-25 17:08:26
原创
1297人浏览过

[PHP]代码   

class Templates{  
    /*获取站点根目录*/  
    var $web_root;  
    /*模板目录*/  
    var $templates_dir;  
    /*编译目录*/  
    var $compile_dir;  
    /*通过它判断读取的是前台模板还是后台模板*/  
    var $templates_postion;  
    /*临时存储模板文件内容*/  
    var $out_put_content;  
    /*临时数组*/  
    var $_array=array();  
    var $left_tag='[';  
    var $right_tag=']';  
    /* 
    *其它类对象 
    */  
    var $class_obj;  
    /* 
    *构造函数初始化数据 
    */  
    function __construct($v)  
    {  
        $this->web_root=$_SERVER['DOCUMENT_ROOT'].'/';  
        $this->templates_postion=$v;  
    }  
    /* 
    读取XML文件获取相应的模板与编译的目录地址 
    */  
    function read_site_config_xml()  
    {  
        /* 
        定义读取XML 
        $xml_dom; 
        */  
        $xml_dom=simplexml_load_file($this->web_root.'web.config.xml');  
        $this->compile_dir=$this->web_root.$xml_dom->compileConfig->dir;  
        switch($this->templates_postion)  
        {  
            case 'front':  
            $this->templates_dir=$this->web_root.$xml_dom->templatesConfig->templatesDir;  
            break;  
            case 'admin':  
            $this->templates_dir=$this->web_root.$xml_dom->templatesConfig->adminTemplatesDir;  
            $this->compile_dir.='admin/';  
            break;  
        }  
        unset($xml_dom);  
    }  
    /* 
    读取模板文件 
    */  
    function read_template($templatename)  
    {  
        $this->read_site_config_xml();  
        return file_get_contents($this->templates_dir.$templatename);  
    }  
    /* 
    注册模板变量 
    */  
    function assign($tag,$tagvalue=null)  
    {  
        if(is_array($tag))  
        {  
            foreach($tag as $tagkey=>$tagkeyvalue)  
            {  
                $this->_array[$tagkey]=$tagkeyvalue;  
            }  
        }  
        else  
        {  
            $this->_array[$tag]=$tagvalue;  
        }  
    }  
    /* 
    向模板中赋值 
    */  
    function assgin_value()  
    {  
        foreach($this->_array as $tag=>$tagvalue)  
        {  
                $this->out_put_content=str_replace($this->left_tag.$tag.$this->right_tag,'$this->_array[\''.$tag.'\']',$this->out_put_content);  
        }  
    }  
    /* 
    输出模板文件 
    */  
    function out_put($templatename)  
    {  
        //先检查此文件是否已编译,如果编译则直接输出  
        if($this->check_compile($templatename))  
        {  
            echo $this->out_put_content;  
        }  
        else  
        {  
            $this->out_put_content=$this->read_template($templatename);  
            $this->convert_php_tag();  
            $this->assgin_value();  
            $this->make_compile($templatename);  
            echo $this->out_put_content;  
        }  
    }  
    /* 
    编译模板 
    */  
    function make_compile($compilename)  
    {  
        $name=$this->compile_dir.$compilename.'.php';  
        file_put_contents($name,$this->out_put_content,LOCK_EX);  
        ob_start();  
        include($name);  
        $this->out_put_content=ob_get_contents();  
        ob_end_clean();       
    }  
    /* 
    检查编译 
    */  
    function check_compile($compilename)  
    {  
        $this->read_site_config_xml();  
        $name=$this->compile_dir.$compilename.'.php';  
        if(file_exists($name))  
        {  
            ob_start();  
            include($name);  
            $this->out_put_content=ob_get_contents();  
            ob_end_clean();  
            $returnvalue=true;  
        }  
        else  
        {  
            $returnvalue=false;  
        }  
        return $returnvalue;  
    }  
    /* 
    处理模板文件中PHP语法 
    */  
    function convert_php_tag()  
    {  
        $html_file_tag=array  
        (  
            /*结构控制语句开始、结束标签*/  
            '<{foreach' => '<?php foreach',  
            '<{if' => '<?php if',  
            '<{for'=>'<?php for',  
            ':}>'=>': ?>',  
            '<{/if}>'=>'<?php endif; ?>',  
            '<{/else}>'=>'<?php else: ?>',  
            '<{/foreach}>'=>'<?php endforeach; ?>',  
            '<{/for}>'=>'<?php endfor; ?>',  
            /*输出数组变量内容标签*/  
            '<${'=>'<?php echo ',  
            '}$>'=>'; ?>',  
            /*加载文件标签*/  
            '</{'=>'<?php include($this->web_root.\'',  
            '}/>'=>'\'); ?>',  
              
            '<{'=>'<?php ',  
            '}>'=>' ;?>'  
        );  
        foreach($html_file_tag as $key=>$keyvalue)  
        {  
            $this->out_put_content=str_replace($key,$keyvalue,$this->out_put_content);  
        }  
    }  
    /* 
    *引用其它类文件 
    */  
    function include_class($classname)  
    {  
        @include($this->web_root.'core/'.$classname.'.php');  
        $this->class_obj=new $classname();  
    }  
    /* 
    释放资源 
    */  
    function __destruct()  
    {  
        unset($this->out_put_content);  
        unset($this->_array);  
        unset($this->class_obj);  
    }  
}  
?>
登录后复制

                   

Devv
Devv

Devv是一个专为程序员打造的新一代AI搜索引擎

Devv 140
查看详情 Devv

                   

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号