使用 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
}
}
这段代码是否正确,因为尽管页脚和头文件不为空,但它没有产生任何输出。
$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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我敢说是因为这条线
执行后,
$this->includes
将包含单个整数或布尔值false
请参阅http://php.net/manual/en/function.preg-match-all.php
I dare say it's because of this line
After that is executed,
$this->includes
will contain either a single integer or booleanfalse
See http://php.net/manual/en/function.preg-match-all.php
我想知道这一行是否符合您的预期:
preg_match_all
返回匹配数。您将它作为第三个参数传递并分配它。另外,我想你错过了这里的引号:
I wonder if this line does what you intend:
preg_match_all
returns the number of matches. You passed it as third parameter and assigned it.Additionally, I suppose you missed quotes here: