wir
9/17/2017 - 12:54 PM

A function to render a php file with data. Allows templating and then sending an array of data into the view.

A function to render a php file with data. Allows templating and then sending an array of data into the view.

function renderPhpFile($filename, $vars = null) {
  if (is_array($vars) && !empty($vars)) {
    extract($vars);
  }
  ob_start();
  include $filename;
  return ob_get_clean();
}

// usage
echo renderPhpFile('views/templates/index.php', $data_for_view);