Last active
August 19, 2023 03:34
-
-
Save stnc/cc23d946f5e847dc60bf117b2680612a to your computer and use it in GitHub Desktop.
PHP capture print/require output in variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function renderHtml($name) { | |
$path = $name; | |
if (file_exists($path) == false) { | |
throw new Exception('View not found in '. $path); | |
return false; | |
} | |
ob_start(); | |
require($path); | |
$output = ob_get_clean(); | |
return $output; | |
} | |
file_put_contents("template.html", renderHtml("body.php")); | |
echo renderHtml("body.php"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$content = '<div id="top"></div>'; | |
$content.= renderHtml('title.php'); | |
print($content); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$content = '<h1>Page heading</h1>'; | |
print($content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment