【问题标题】:templating in php using tpl files使用 tpl 文件在 php 中进行模板化
【发布时间】:2011-02-15 23:54:57
【问题描述】:
$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
        }

    }

这段代码是否正确,因为尽管页脚和头文件不为空,但它没有产生任何输出。

【问题讨论】:

    标签: php templating


    【解决方案1】:

    我敢说是因为这条线

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

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

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

    【讨论】:

      【解决方案2】:

      我想知道这条线是否符合您的预期:

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

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

      另外,我想你错过了这里的报价:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多