This is a rough idea (code taken and modified from one of my own frameworks, I built a long ago, only to clarify my understanding), you may create a View
class and put this function as method, but this function could be used as
$content = render('view_name', array('name' => 'Heera', 'age' => '101'));
Function render
:
function render( $filename, $data = array() )
{
try {
$file = 'views/' . $filename. '.php';
if( !is_readable($file) ){
throw new Exception("View $file not found!", 1);
}
$content = file_get_contents( $file );
ob_start() && extract($data, EXTR_SKIP);
eval('?>'.$content);
$content = ob_get_clean();
ob_flush();
return $content;
} catch (Exception $e) {
return $e->getMessage();
}
}
You can think of a view like this
<div><?php echo $name ?></div>
<div><?php echo $age ?></div>
You can follow some existing frameworks to (I did when I developed this one and helped me a lot) and write your own.