Second argument in render
Sep 7, 2022
3
Level 1
Blade::render() with data?
Hi,
I am currently rendering a view using html passed in from a db table using the following code:
$html = replace_tags_helper($data->source);
file_put_contents(resource_path("/views/".$this->file.".blade.php"), trim($html));
return view($this->file)->with('data', $data);
This is creating a temporary view file and rendering it. This makes me doubt it's performance.
I stumbled across Blade::render(). I hoped that I could use this instead of creating a physical view file.
I have tried passing data into this but i could not get it to work.
$html = replace_tags_helper($data->source);
return Blade::render($html)->with('data', $data);
Does anyone know how If its possible to pass $data into blade::render or have an alternative suggestion to how i could achive this?
Thanks
Level 104
Yes, it is the same as passing data to a View:
return Blade::render($html, ['data' => $data]);
https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates
2 likes
Please or to participate in this conversation.