>  기사  >  백엔드 개발  >  php简单的自定义模板类_PHP教程

php简单的自定义模板类_PHP教程

WBOY
WBOY원래의
2016-07-13 17:46:231162검색

    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的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478594.htmlTechArticle?php class MyTpl { private $template_dir; private $compile_dir; private $tpl_vars=array(); public function __construct($template_dir=./templates, $compile_dir=./templates_c){ $this...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.