Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MoFish's avatar

Get file_get_contents of a View.

Hi,

Is there an easy way via Laravel to do a file_get_contents() of a view so it can be rendered in a textarea for manipulation? Some frameworks such as code ignitor allow you to receive the view's source code without rendering it.

I tried $html = View::make('admin.templates.show')->render() without success, but think the render is probably throwing it off track and causing errors? Would a file_get_contents be the answer? e.g something like.

$html = file_get_contents('\resources\views\admin\templates\show.blade.php');

Thanks,

MoFish

0 likes
6 replies
click's avatar

It depends what you want, do you want a rendered view or the raw content of a blade file?

Your first suggestion parses the template with all of the given parameters and includes, loops, extends etc (equal to the html output). Your second suggestion gives you the raw blade content so including all the unparsed blade syntax like {{ $variable }}, @foreach(), @if() etc.

MoFish's avatar

Hi,

I would like the second option, raw content of the blade file. I tried the file get contents however think my path may be incorrect as that did not work. Could you confirm file_get_contents would be the best method using laravel?

Thanks, Mofish

click's avatar
click
Best Answer
Level 35

@MoFish you should use absolute paths when you want to work with files. file_get_contents() is fine. There are some file wrappers available in laravel but internally they all use file_get_contents().

Try:

file_get_contents(resource_path('views/admin/templates/show.blade.php'));

resource_path() is one of the many helper functions of Laravel. It gives you the absolute path to your resources directory. See https://laravel.com/docs/5.5/helpers#method-resource-path for more methods like this.

2 likes
MoFish's avatar

Thanks @m-rk, that was exactly my issue.

Please or to participate in this conversation.