Search code examples
laravellaravel-8laravel-9

How to create a rendered view from a string instead of a blade file?


I've some html with {{ soimething }} placeholders in a table.

I would like to get the rendered view from this custom html. I would like to avoid to manually do string replace. Is it possible?

Note : I seen suggested questions but I ended finding a more concise way to reach my goal. So I post an answer to this question. Please keep this open.


Solution

  • You can use Blade Facade.

    use Illuminate\Support\Facades\Blade;

    use Illuminate\Support\Facades\Blade;
    
        public function __invoke()
        {
            $name='Peter Pan';
            return Blade::render("
            <h1> Hello {$name} </h1>
            ",['name'=>$name]);
        }