问题
$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