Home > php教程 > php手册 > body text

php简单的自定义模板类

WBOY
Release: 2016-06-13 10:44:57
Original
712 people have browsed it

    class MyTpl { 
        private $template_dir; 
        private $compile_dir; 
        private $tpl_vars=array(); 
 
        public function __construct($template_dir="./templates", $compile_dir="./templates_c"){ 
            $this->template_dir=rtrim($template_dir,"/").'/'; 
            $this->compile_dir=rtrim($compile_dir, "/").'/'; 
        } 
 
        public function assign($tpl_var, $value=null){ 
            if($tpl_var!="") 
                $this->tpl_vars[$tpl_var]=$value; 
        } 
 
        public function display($fileName){ 
            $tplFile=$this->template_dir.$fileName; 
 
            if(!file_exists($tplFile)){ 
                return false; 
            } 
 
            $comFileName=$this->compile_dir."com_".$fileName.".php"; 
 
            if(!file_exists($comFileName) || filemtime($comFileName)                 $repContent=$this->tpl_replace(file_get_contents($tplFile)); 
 
                file_put_contents($comFileName, $repContent);    
            } 
 
            include $comFileName; 
             
        } 
 
        private function tpl_replace($content){ 
                $pattern=array( 
                        '/\/i' 
                    ); 
 
                $replacement=array( 
                    'tpl_vars["${1}"]; ?>' 
                    ); 
             
                $repContent=preg_replace($pattern, $replacement, $content); 
 
                return $repContent; 
        } 
 
    } 
?> 
摘自:enough_br的专栏

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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!