Question

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?

Was it helpful?

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();

OTHER TIPS

The way to do it depends on the framework you're using.

But this can be done with PHP by just using nested includes.

For example

page.php

<?php include(HEADER) ?>

<?= $var ?>

<?php include(FOOTER) ?>

All variables available to page.php will be available to the header and footer views as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top