Laravel by default uses Blade Template Engine which works with files with extension “.blade.php” and are readily parsed without any issue. But if you somehow have to parse the Blade tags you fetched from database or some other source than you might have to look into the Blade API for help.
But that would only be if you don’t get the idea from this post.
The basic concept here is to take the blade embedded string along with parameters and parse the string to get the PHP version of the code and then flush it to the browser using PHP output buffer functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static function parseBladeCode($string,array $args=array()){ $generated = Blade::compileString($string); ob_start(); extract($args,EXTR_SKIP); try { eval('?>'.$generated); } catch (\Exception $e) { ob_get_clean(); throw $e; } $content = ob_get_clean(); return $content; } |
Here compileString() is the default method used to convert the Blade code to PHP code. We also extract the $args array to make variables available to PHP and then we eval to compiled code.
At last we return the content to the browser using output buffer.
Warning: Make sure this code does not process user input since it uses eval and we all know the risk it poses.
alexey
Latest posts by alexey (see all)
- Zend Soap NULL Reponse - August 29, 2018
- Quick Facts About WooCommerce - April 25, 2018
- WordPress stuck on a “Too many redirects” error loop when using SSL - January 17, 2018
Recent Comments