使用tpl文件在php中模板化

使用tpl文件在php中模板化

问题描述:

$data = {include "header.tpl"}{include "footer.tpl"};

private function get_tpl_includes($data){
        $this->includes = preg_match_all('/{include \"[^}]\"*}/', $data, $this->includes);

        foreach($this->includes as $include){
            $tpl_file = $this->dir . str_replace($this->dir, "", $include[0]);
            $html_include = file_get_contents($tpl_file) or die("tp3"); //Get the content of the included html
            $pattern = '{include "' . $tpl_file . '"}'; //Create a pattern to replace in the html
            $this->html = str_ireplace($pattern, "", $this->html); //Replace the file include pattern with html
        }

    }

is this code right because it is not producing any output although footer and header files are not empty.

  $ data = {include“header.tpl”} {include“footer.tpl”}; \  n 
private函数get_tpl_includes($ data){
 $ this-> includes = preg_match_all('/ {include \“[^}] \”*} /',$ data,$ this-> includes); \  n 
 foreach($ this-> include as include include){
 $ tpl_file = $ this-> dir。  str_replace($ this-> dir,“”,$ include [0]); 
 $ html_include = file_get_contents($ tpl_file)或die(“tp3”);  //获取包含的html 
 $ pattern ='{include“'。$ tpl_file。'”}}的内容;  //在html 
 $ this-> html = str_ireplace($ pattern,“”,$ this-> html)中创建要替换的模式;  //用html 
替换文件包含模式
 
} 
  code>  pre> 
 
 

此代码是正确的,因为尽管页脚和头文件没有产生任何输出 不是空的。 p> div>

I dare say it's because of this line

$this->includes = preg_match_all('/{include \"[^}]\"*}/', $data, $this->includes);

After that is executed, $this->includes will contain either a single integer or boolean false

See http://php.net/manual/en/function.preg-match-all.php

I wonder if this line does what you intend:

$this->includes = preg_match_all('/{include \"[^}]\"*}/', $data, $this->includes);

preg_match_all returns the number of matches. You passed it as third parameter and assigned it.

Additionally, I suppose you missed quotes here:

$data = '{include "header.tpl"}{include "footer.tpl"}';