Saving HTML contents of view to disk Hi folks!
I tried to dump my routes for static page generation like this:
Route::get('/', function()
{
File::put(public_path() . '/static/index.html', View::make('index'));
});
The file is created but empty. How can I dump the generated HTML to the file. When just returning the view (either via view('...'); or View::make('...');) my page looks good.
You can do it using
$html = View::make('index')->render();
// Or
$html = view('index')->render();
That will return the html itself
I tried both but the file is still empty. It's not a restriction problem since the file can be created + I am on XAMPP on Windows.
No clue where this comes from.
How about testing with file_put_contents()?
file_put_contents() also produces an empty file.
Edit: If I use hardcoded text it works. But the rendered HTML is not getting stored (file_put_contents() and File::put())
What is the content of your view ?
Have you tested the ->render() returns a string?
@pmall My view is simply a blade file which contains 2 sections and extends to the master blade template. So kinda like this:
<!--master.blade.php-->
<DOCTYPE html>
<html>
<head>
....
<title>@yield ('title')</title>
...
</head>
<body>
<div>
...
@yield ('content')
</div>
</body>
</html>
And the extending blade:
<!--index.blade.php-->
@extends ('master')
@section ('title', 'Inndex')
@section ('content')
<div>
...
</div>
@stop
@bashy : From the docs (http://laravel.com/api/5.1/Illuminate/View/View.html#method_render ) it should but testing like this:
$html = view('index')->render();
$res = $html === '' ? 'true' : 'false';
return $res;
returns the page in the web browser but not true or false
So render seems like to be echoing the string instead of returning it.
What if you do this ?
ob_start();
view('index')->render();
$content = ob_get_contents();
ob_end_clean();
File::put(public_path() . '/static/index.html', $content);
It sadly creates an empty index.html. Man I think I will just save the f*cking sites via Firebug.
Seems there another issue with your code somewhere or you haven't tested the saving of the file. Permissions?
This works
$view = view('view.name');
$contents = (string) $view;
// or
$contents = $view->render();
file_put_contents('filename.html', $contents);
@bashy : I don't think it is an issue with persmission since something like
file_put_contents(public_path() . '/static/index.html', "lsdflaflöksd");
works.
I think I will try to get a fresh install of laravel. Something is fishy here.
Yeah sounds like a good idea, very strange!
Please sign in or create an account to participate in this conversation.