Search code examples
phpmodel-view-controllerincludeoutput-buffering

how to pre-process a PHP MVC view without using the framework?


Is there any way I can pre-process a PHP view script without using a particular MVC framework?

Basically I want to render the view and pass it as an HTML string to another view. The view I'm trying to render has some references like $this->rows, and, of course, I would need to add the values of those references to the script before generating the HTML.

Is this possible?


Solution

  • Yes it is completely possible. You'll want to utilize output buffering to ensure the initial view isn't displayed and then store that views output in a variable.

    ob_start();
    include ('/path/to/file.php');
    $contents = ob_get_contents();
    ob_end_clean();