john010117 Posted September 28, 2007 Share Posted September 28, 2007 I have managed to build a small template class that can so far parse template variables. Now, to move one step further, I have begun to put in a template include part (includes separate templates within a template file). Here's what I have so far (only the include parts are shown): <?php class template { var $template_root = './template'; var $output; function _include_template($filename) { ob_start(); include($this->template_root . '/' . $filename); $content = ob_get_contents(); ob_end_clean(); return $content; } function compile($filename) { $this->output = @file_get_contents($this->template_root . '/' . $filename); // Locates all the include statements (looks like: <!-- INCLUDE some_file.html -->) preg_match_all('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->output, $include_matches); if(!empty($include_matches[1])) { for($i = 0, $size = sizeof($include_matches[1]); $i < $size; $i++) { $this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($include_matches[1][0]), $this->output); array_shift($include_matches[1]); } } echo $this->display(); } function display() { return $this->output; } } ?> If I include one file in a template file, the file shows up fine. However, if I include two or more files, the first file that was included in the template keeps on getting included over and over again. Does anyone see a problem with this code? Help will be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/ Share on other sites More sharing options...
teng84 Posted September 28, 2007 Share Posted September 28, 2007 explain ! However, if I include two or more files, the first file that was included in the template keeps on getting included over and over again. Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-356914 Share on other sites More sharing options...
john010117 Posted September 28, 2007 Author Share Posted September 28, 2007 explain ! However, if I include two or more files, the first file that was included in the template keeps on getting included over and over again. What I mean is this: Let's say I had the template file "index_body.html" (called from index.php). Then, in that template file, I include "index_2.html". So far, so good. But, if I include "index_3.html" or any other file below the first include statement, only "index_2.html" shows up within "index_body.html". That happens the number of include statements (stated in index_body.html) times. Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-356921 Share on other sites More sharing options...
teng84 Posted September 28, 2007 Share Posted September 28, 2007 i believe the problem is inside the loop because you put the value in the array but i dont see a code that will call the index of that array from preg match eg.. $array[ist ], $array[2nd ] etc... Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-356923 Share on other sites More sharing options...
john010117 Posted September 28, 2007 Author Share Posted September 28, 2007 i believe the problem is inside the loop because you put the value in the array but i dont see a code that will call the index of that array from preg match eg.. $array[ist ], $array[2nd ] etc... Can you please elaborate on that a bit more? I'm confused as to what you're implying. Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-356926 Share on other sites More sharing options...
john010117 Posted September 28, 2007 Author Share Posted September 28, 2007 I'm sorry for double-posting, but I'm still in need of help. I have tried other array functions, but most of them returns the same result. Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-357585 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 maybe instead of for($i = 0, $size = sizeof($include_matches[1]); $i < $size; $i++) { $this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($include_matches[1][0]), $this->output); array_shift($include_matches[1]); } try foreach($include_matches[1] as $IM) { $this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($IM), $this->output); } Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-357586 Share on other sites More sharing options...
john010117 Posted September 28, 2007 Author Share Posted September 28, 2007 Ok, I tried that method, but that outputted no difference. To make sure, I've echo'ed the value of $IM within the loop, and it shows what it's supposed to show. Here's the updated code: <?php class template { var $template_root = './template'; var $output; function _include_template($filename) { ob_start(); include($this->template_root . '/' . $filename); $content = ob_get_contents(); ob_end_clean(); return $content; } function compile($filename) { $this->output = @file_get_contents($this->template_root . '/' . $filename); preg_match_all('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->output, $include_matches); if(!empty($include_matches[1])) { foreach($include_matches[1] as $IM) { $this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($IM), $this->output); } } echo $this->display(); } function display() { return $this->output; } } ?> I'm just guessing, but could the problem lie in the _include_template() function with the output buffering? Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-357597 Share on other sites More sharing options...
MadTechie Posted September 29, 2007 Share Posted September 29, 2007 the logic isn't right.. $this->output = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+) -->#', $this->_include_template($IM), $this->output); should be $this->output = preg_replace($IM, $this->_include_template($IM), $this->output); Warning: i haven't had much sleep! Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-357614 Share on other sites More sharing options...
john010117 Posted September 29, 2007 Author Share Posted September 29, 2007 Ok, thank you very much, Mad Techie and teng84. The last solution solved my problem. I appreciate all of your time. Quote Link to comment https://forums.phpfreaks.com/topic/70981-solved-help-with-template-class-include-statements/#findComment-357618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.