templating in php using tpl files

删除回忆录丶 提交于 2019-12-24 11:55:01

问题


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


回答1:


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




回答2:


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"}';


来源:https://stackoverflow.com/questions/5010925/templating-in-php-using-tpl-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!