0

I have template files which I need to further process so would like to assign it to a string variable using an include not using file_get_contents. The reason for this is because the final output is buffered through ob and contains php which needs to execute. If I use file_get_contents it wont execute. Problem is if I use $output = include 'file.php' it prints output without assigning.

5
  • Where's the output buffering - the included script or the main script? Commented Apr 25, 2013 at 2:53
  • Add another layer of output buffering, and use ob_get_clean() Commented Apr 25, 2013 at 2:54
  • the main script call a class to set up the page and process the templates. In the main page it call this class, processes output and I use ob_start() //$template = ob_get_contents(); //ob_clean(); //$registry->getObject('template')->parseOutput(); //$template = $registry->getObject('template')->getPage()->getContentToPrint(); //$template = str_replace(array("\r\n","\n","\r"),'',$template); //echo $template; //ob_end_flush(); Commented Apr 25, 2013 at 2:56
  • $template = ob_get_contents(); ob_clean(); $registry->getObject('template')->parseOutput(); $template = $registry->getObject('template')->getPage()->getContentToPrint(); $template = str_replace(array("\r\n","\n","\r"),'',$template); echo $template; ob_end_flush(); sorry I had lines commented for testing Commented Apr 25, 2013 at 2:57
  • 1
    I believe the answer below by CORRUPT is exactly what you want, but just to note, if you use a url in file_get_contents() instead of a filepath, the php should execute. Commented Apr 25, 2013 at 3:09

1 Answer 1

2

Try this:

ob_start();

include 'file.php';

$output = ob_get_clean();
Sign up to request clarification or add additional context in comments.

1 Comment

That was the method I was using before I decided to do the processing in a class. Reason I decided to do the class was to better organize, process and store the templates. However I did not know that using a URL instead of path for file_get_contents would allow the php to execute. Thanks for the tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.