Explicitly parsing Blade code in Laravel 4

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.

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

alexey

Application developer, movie buff, occasional reader and a huge Manchester United fan.
alexey

Application developer, movie buff, occasional reader and a huge Manchester United fan.