使用tpl文件在php中模板化

时间:2011-02-15 23:54:57

标签: php templating

$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
        }

    }

这个代码是正确的,因为虽然页脚和头文件不为空,但它没有产生任何输出。

2 个答案:

答案 0 :(得分:1)

我敢说这是因为这条线

  

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

执行该操作后,$this->includes将包含单个整数或布尔值false

请参阅http://php.net/manual/en/function.preg-match-all.php

答案 1 :(得分:0)

我想知道这条线是否符合您的意图:

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

preg_match_all返回匹配数。您将其作为第三个参数传递并分配。

此外,我想你在这里错过了引用:

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