<?php <br /> <?php $this->_include('plik_testowy.tpl'); ?> <!-- BEGIN test --> {test.AAA} <!-- END test -->',$matches2); ?>
Wyświetla "!ok". Print_r($matches2) daje tablicę złożoną z 2 pustych tablic. Co jest źle?
I $pattern, $this->content są takie same jak w preg_match_all.
Cały plik (błąd występuje w template_compile::parse_loops):
tpl/test.tpl:
<?php class template_compile { public function __construct($dir) { $this->dir = $dir; return true; } public function compile() { foreach ($files as $file_id => $file_name) { $this->compile_exec($file_name); } return true; } public function compile_file($file_name) { $this->compile_exec($tis->dir . 'tpl_' . $file . '.tpl'); return true; } private function compile_exec($file) { { } $this->parse_vars(); $this->parse_includes(); $this->parse_ifs(); $this->parse_loops(); return true; } private function parse_vars() { foreach ($matches[0] as $id => $search) { { $this->content = str_replace($search,'<?php echo $this->_VARS['' . $matches[1][$id] . '']; ?>',$this->content); } } return true; } private function parse_includes() { foreach ($matches[1] as $id => $file) { $this->content = str_replace($matches[0][$id],'<?php $this->_include('' . $file . ''); ?>',$this->content); } return true; } private function parse_ifs() { foreach ($matches[1] as $id => $if) { // } return true; } private function parse_loops() { foreach ($matches[1] as $id => $loop_name) { $pattern = '/<!-- BEGIN ' . $loop_name . ' -->(.*?)<!-- END ' . $loop_name . ' -->/';echo $pattern; echo $this->content; preg_match_all($pattern,$this->content,$matches2);if(preg_match($pattern,$this->content)){echo 'ok';}else{echo '!ok';} die; foreach ($matches2[1] as $id2 => $code) { $new_code = $code; foreach ($matches2[1] as $id123 => $search) { { $new_code = str_replace($search,'<?php echo $this->_BLOCK_VARS[' . $loop_name . '][' . $matches2[1][$id123] . ']; ?>',$new_code); } } $this->content = preg_replace('/<!-- BEGIN ' . $loop_name . ' -->' . $new_code . '<!-- END ' . $loop_name . ' -->/','<?php foreach ($this->_BLOCK_VARS['' . $loop_name . ''] as $id => $value){eval('<?php' . ' . $new_code . ' . '?>');} ?>',$this->content); } } return true; } } ?> <?php $tc = new template_compile('tpl/'); $tc->_VARS['test'] = 'zmienne: działają'; 'test.AAA' => 'loopy ok' ); $tc->compile(); ?>
Kod
{TEST}
<!-- INCLUDE plik_testowy.tpl -->
<!-- BEGIN test -->
fghj{test.AAA}aa
<!-- END test -->
<!-- INCLUDE plik_testowy.tpl -->
<!-- BEGIN test -->
fghj{test.AAA}aa
<!-- END test -->