templating in php using tpl files

645 Views Asked by At
$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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

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