Skip to content

Instantly share code, notes, and snippets.

@AlexR1712
Last active December 5, 2022 07:20
Show Gist options
  • Save AlexR1712/83e502e25300fcfeb4199b88f3bdce49 to your computer and use it in GitHub Desktop.
Save AlexR1712/83e502e25300fcfeb4199b88f3bdce49 to your computer and use it in GitHub Desktop.
Compile a string like Blade Template in Laravel
<?php
// Only you need to add in your controller.
public function bladeCompile($value, array $args = array())
{
$generated = \Blade::compileString($value);
ob_start() and extract($args, EXTR_SKIP);
// We'll include the view contents for parsing within a catcher
// so we can avoid any WSOD errors. If an exception occurs we
// will throw it out to the exception handler.
try
{
eval('?>'.$generated);
}
// If we caught an exception, we'll silently flush the output
// buffer so that no partially rendered views get thrown out
// to the client and confuse the user with junk.
catch (\Exception $e)
{
ob_get_clean(); throw $e;
}
$content = ob_get_clean();
return $content;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment