Last active
December 5, 2022 07:20
-
-
Save AlexR1712/83e502e25300fcfeb4199b88f3bdce49 to your computer and use it in GitHub Desktop.
Compile a string like Blade Template in Laravel
This file contains 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 | |
// 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
See also https://laracasts.com/discuss/channels/general-discussion/render-template-from-blade-template-in-database