How can I compile a blade template from a string rather than a view file, like the code below:

<?php
$string = '<h2>{{ $name }}</h2>';
echo Blade::compile($string, array('name' => 'John Doe')); 
?>

http://paste.laravel.com/ujL

upvote
  flag
The question should be, why would you even need to do this? – David Barker
upvote
  flag
You've written some code that looks good - what's wrong with it? – Phill Sparks
upvote
  flag
I have tried $string = '<h2>{{ $name }}</h2>'; echo Blade::compileString($string, array('name' => 'John Doe')); nothing returned. – Tee Plus
upvote
  flag
This is almost a duplicate of //allinonescript.com/questions/21556981/… Just pointing the link where you would find more solutions – Heroselohim
6 upvote
  flag
David Barker, it should be a StackExchange policy that people who respond just to lecture the asker about what question they should be asking instead, should have all their SE accounts terminated, all their known email addresses blacklisted for life, and all their contributions scrubbed. It's precisely that annoying. – Szczepan Hołyszewski

4 Answers 11

I'm not using blade this way but I thought that the compile method accepts only a view as argument.

Maybe you're looking for:

Blade::compileString()
upvote
  flag
$string = '<h2>{{ $name }}</h2>'; echo Blade::compileString($string, array('name' => 'John Doe')); "name" is not assign to the template. – Tee Plus

I found the solution by extending BladeCompiler.

<?php namespace Laravel\Enhanced;

use Illuminate\View\Compilers\BladeCompiler as LaravelBladeCompiler;

class BladeCompiler extends LaravelBladeCompiler {

    /**
     * Compile blade template with passing arguments.
     *
     * @param string $value HTML-code including blade
     * @param array $args Array of values used in blade
     * @return string
     */
    public function compileWiths($value, array $args = array())
    {
        $generated = parent::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;
    }

}
upvote
  flag
Thanks it helped a lot. Just to help someone like me I can add to your post that a way to instancing a custom compiler extension is to pass filesystem instance ad path to compiled views to the constructor: $myBlade = new MyBlade(App::make('files'), App::make('path').'/storage/views'); – Felice Ostuni
upvote
  flag
How did you get laravel to use this blade compiler? – mrwaim

Small modification to the above script. You can use this function inside any class without extending the BladeCompiler class.

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;
}

It's a old question. But I found a package which makes the job easier.

Laravel Blade String Compiler renders the blade templates from the string value. Check the documentation on how to install the package.

Here is an example:

$template = '<h1>{{ $name }}</h1>';  // string blade template

return view (['template' => $template], ['name' => 'John Doe']);
upvote
  flag
Very good! Saved my life sir! Thanks! – Thalis Vilela

Not the answer you're looking for? Browse other questions tagged or ask your own question.