Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active April 4, 2024 15:33
Show Gist options
  • Save james2doyle/6378289 to your computer and use it in GitHub Desktop.
Save james2doyle/6378289 to your computer and use it in GitHub Desktop.
A function to render a php file with data. Allows templating and then sending an array of data into the view.
<?php
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);
@palexandrite
Copy link

Wow! Thank you!

@Syltech
Copy link

Syltech commented Oct 20, 2020

Clean ! Thank you !

@ascaroit
Copy link

Nice, thanks mate!

@Leonetienne
Copy link

Leonetienne commented Mar 6, 2021

awesome! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment