ホームページ  >  記事  >  バックエンド開発  >  PHP シンプルなカスタム テンプレート class_PHP チュートリアル

PHP シンプルなカスタム テンプレート class_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 17:46:231162ブラウズ

    クラス MyTpl {
        プライベート $template_dir; 
        プライベート $compile_dir; 
        プライベート $tpl_vars=array(); 
 
        パブリック関数 __construct($template_dir="./templates", $compile_dir="./templates_c"){
            $this->template_dir=rtrim($template_dir,"/").'/'; 
            $this->compile_dir=rtrim($compile_dir, "/").'/'; 
        }
 
        パブリック関数 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)){
                false を返します。 
            }
 
            $comFileName=$this->compile_dir."com_".$fileName.".php"; 
 
            if(!file_exists($comFileName) || filemtime($comFileName) < filemtime($tplFile)){
                $repContent=$this->tpl_replace(file_get_contents($tplFile)); 
 
                file_put_contents($comFileName, $repContent);    
            }
 
            $comFileName を含めます。 
             
        }
 
        プライベート関数 tpl_replace($content){
                $pattern=array(
                        '/<{s*$([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)s*}>/i'
                    ); 
 
                $replacement=array(
                    'tpl_vars["${1}"]; ?>' 
                    ); 
             
                $repContent=preg_replace($pattern, $replacement, $content); 
 
                $repContent を返します。 
        }
 
    }
?> 
抜粋:十分な_br的专栏

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/478594.html技術記事 ?php クラス MyTpl { プライベート $template_dir;プライベート $compile_dir;プライベート $tpl_vars=array(); public function __construct($template_dir=./templates, $compile_dir=./templates_c){ $this...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。