Search code examples
laravellaravel-5laravel-5.3

Laravel 5 - Compile String and Interpolate Using Blade API on Server


Using the Blade service container I want to take a string with markers in it and compile it down so it can be added to the blade template, and further interpolated.

So I have an email string (abridge for brevity) on the server retrieved from the database of:

<p>Welcome {{ $first_name }},</p>

And I want it to interpolated to

<p>Welcome Joe,</p> 

So I can send it to a Blade template as $content and have it render all the content and markup since Blade doesn't interpolate twice and right now our templates are client made and stored in the database.

Blade::compileString(value) produces <p>Welcome <?php echo e($first_name); ?>,</p>, but I can't figure out how to get $first_name to resolve to Joe in the string using the Blade API, and it doesn't do it within the Blade template later. It just displays it in the email as a string with PHP delimiters like:

<p>Welcome <?php echo e($first_name); ?>,</p>

Any suggestions?


Solution

  • This should do it:

    // CustomBladeCompiler.php
    
    use Symfony\Component\Debug\Exception\FatalThrowableError;
    
    class CustomBladeCompiler
    {   
        public static function render($string, $data)
        {
            $php = Blade::compileString($string);
    
            $obLevel = ob_get_level();
            ob_start();
            extract($data, EXTR_SKIP);
    
            try {
                eval('?' . '>' . $php);
            } catch (Exception $e) {
                while (ob_get_level() > $obLevel) ob_end_clean();
                throw $e;
            } catch (Throwable $e) {
                while (ob_get_level() > $obLevel) ob_end_clean();
                throw new FatalThrowableError($e);
            }
    
            return ob_get_clean();
        }
    }
    

    Usage:

    $first_name = 'Joe';
    $dbString = '<p>Welcome {{ $first_name }},</p>';
    
    return CustomBladeCompiler::render($dbString, ['first_name' => $first_name]);
    

    Thanks to @tobia on the Laracasts forums.