Bisa Posted July 29, 2010 Share Posted July 29, 2010 I'm working on a basic structure for websites containing user management and a template engine. I've run into troubles with the template engine when I try to allow for including scripted pages. This function is supposed to include and buffer the scripted page: function parse_file($file) { ob_start(); include($file); $buffer = ob_get_clean(); return $buffer; } In my template class I check to see if the data entered is a file or not, if it is then I run these lines to replace place holder-tags with the output buffer of the file included: foreach ($tags as $tag => $data) { if (file_exists($data)){ $data = $this->parse_file($data); } $this->page = preg_replace('/{' . $tag . '}|<!-- '.$tag.' -->/', $data, $this->page); } now here is the hitch, it seems the preg_replace is only replacing one of several identical place holder-tags and I am puzzled as of why this happens, could it be the buffer or what? Quote Link to comment https://forums.phpfreaks.com/topic/209261-i-cant-figure-how-to-work-output-buffer/ Share on other sites More sharing options...
simshaun Posted July 29, 2010 Share Posted July 29, 2010 If it's replacing at least one placeholder tag, then that means parse_file() is working. (You are using OB the right way) So, are you saying your "template" contains multiple {test} tags (just used test as an example), and only one of them is being replaced? If so, I think the error lies somewhere else in your code, because the loop you provided should work just fine. The only thing I would do to it is wrap the occurences of $tag with preg_quote in your preg_replace call. Quote Link to comment https://forums.phpfreaks.com/topic/209261-i-cant-figure-how-to-work-output-buffer/#findComment-1092728 Share on other sites More sharing options...
Bisa Posted July 29, 2010 Author Share Posted July 29, 2010 I see. And yes, I use {PAGE_TITLE} two times for example. One time in the header.html that I am including (the html gets printed out just fine) and a second time in the body. The only time I actually get the title printed is in the body where it's preg_replaced based on a tag associative array " Title" => "My Page Title" what seems to be the issue here is that whenever Ive included a file such as my header.html the preg_replace won't replace my {PAGE_TITLE} which should be located in the buffer output string - instead it just skips it and I'm left with the place holder being echoed rather then my desired title. Quote Link to comment https://forums.phpfreaks.com/topic/209261-i-cant-figure-how-to-work-output-buffer/#findComment-1092733 Share on other sites More sharing options...
simshaun Posted July 29, 2010 Share Posted July 29, 2010 Hmm wait. What's the structure of $tags? (do a print_r on it and paste) I would assume it would be like you said, array('title' => 'My Page Title', 'meta_keywords' => 'foo, bar'). If so, why are you trying to load the value of each element as if it were a file. I was mistaken above.. now I do think your logic is flawed in the loop. You should: 1. Parse (load) the file before the loop (i.e. $this->page = $this->parse_file('header.html') ). 2. Loop through $tags. It should contain the preg_replace call you already use, but not the if file exists control structure. Quote Link to comment https://forums.phpfreaks.com/topic/209261-i-cant-figure-how-to-work-output-buffer/#findComment-1092755 Share on other sites More sharing options...
Bisa Posted July 29, 2010 Author Share Posted July 29, 2010 $tags print_r results in this: Array ( [PAGE_TITLE] => Index [PAGE_HEAD] => ./templates/page_head.html [PAGE_FOOT] => ./templates/page_foot.html ) 1 and by that you might figure that I cannot simply parse the file before the loop as I would need to parse both head and foot thus making the buffer consist of only head and foot, or am I thinking wrong there? Quote Link to comment https://forums.phpfreaks.com/topic/209261-i-cant-figure-how-to-work-output-buffer/#findComment-1092762 Share on other sites More sharing options...
Bisa Posted July 29, 2010 Author Share Posted July 29, 2010 Actually, yes, the loop logic was kinda flawed, what I realized due to your input was the following: Since the {PAGE_TITLE} had already been handled in the first circulation it couldn't be replaced magically in some way once the files had been included which also contained {PAGE_TITLE}. What I did to solve this was, as u suggested, simply to include the pages first and then loop through the tags that had only replacements in it for them Thnx Quote Link to comment https://forums.phpfreaks.com/topic/209261-i-cant-figure-how-to-work-output-buffer/#findComment-1092767 Share on other sites More sharing options...
simshaun Posted July 29, 2010 Share Posted July 29, 2010 Ok I see what you are trying to do. Now we're back to square 1. I'm fairly certain it's a problem somewhere else in your code. <?php function parse_file($file) { ob_start(); include($file); $buffer = ob_get_clean(); return $buffer; } $tags = array('header' => 'header.php', 'footer' => 'footer.php', 'title' => 'Index'); $template = '{header} <h1><!-- title --></h1> <p>Hello world</p> {footer}'; echo '<pre>'; foreach ($tags AS $tag => $data) { if (is_file($data)) { $data = parse_file($data); } $template = preg_replace('/{'. preg_quote($tag,'/') .'}|<!-- '. preg_quote($tag,'/') .' -->/', $data, $template); echo "\n\nTag: $tag"; echo "\nData: $data"; } echo '</pre>'; echo '<hr>'; echo $template; That is basically what you are doing. header.php contains Foo and footer.php contains Bar. Works just fine. Edit: Glad you got it worked out. Quote Link to comment https://forums.phpfreaks.com/topic/209261-i-cant-figure-how-to-work-output-buffer/#findComment-1092769 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.