Home > php教程 > PHP源码 > 读取、把模板中的标签解析成PHP语法编译成PHP文件

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

PHP中文网
Release: 2016-05-25 17:08:26
Original
1150 people have browsed it

[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  
        (  
            /*结构控制语句开始、结束标签*/  
            &#39;<{foreach&#39; => &#39;<?php foreach&#39;,  
            &#39;<{if&#39; => &#39;<?php if&#39;,  
            &#39;<{for&#39;=>&#39;<?php for&#39;,  
            &#39;:}>&#39;=>&#39;: ?>&#39;,  
            &#39;<{/if}>&#39;=>&#39;<?php endif; ?>&#39;,  
            &#39;<{/else}>&#39;=>&#39;<?php else: ?>&#39;,  
            &#39;<{/foreach}>&#39;=>&#39;<?php endforeach; ?>&#39;,  
            &#39;<{/for}>&#39;=>&#39;<?php endfor; ?>&#39;,  
            /*输出数组变量内容标签*/  
            &#39;<${&#39;=>&#39;<?php echo &#39;,  
            &#39;}$>&#39;=>&#39;; ?>&#39;,  
            /*加载文件标签*/  
            &#39;</{&#39;=>&#39;<?php include($this->web_root.\&#39;&#39;,  
            &#39;}/>&#39;=>&#39;\&#39;); ?>&#39;,  
              
            &#39;<{&#39;=>&#39;<?php &#39;,  
            &#39;}>&#39;=>&#39; ;?>&#39;  
        );  
        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.&#39;core/&#39;.$classname.&#39;.php&#39;);  
        $this->class_obj=new $classname();  
    }  
    /* 
    释放资源 
    */  
    function __destruct()  
    {  
        unset($this->out_put_content);  
        unset($this->_array);  
        unset($this->class_obj);  
    }  
}  
?>
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template