使用 tpl 文件在 php 中进行模板化

发布于 2024-10-18 03:10:51 字数 740 浏览 1 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2024-10-25 03:10:51

我敢说是因为这条线

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

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

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

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

萌梦深 2024-10-25 03:10:51

我想知道这一行是否符合您的预期:

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

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

另外,我想你错过了这里的引号:

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

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"}';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文