Skip to content

Commit

Permalink
Merge pull request #79 from plazareff/master
Browse files Browse the repository at this point in the history
Add specific View Builder case to documentation
  • Loading branch information
josegonzalez authored Mar 16, 2017
2 parents e3b5ebb + ec6e7ac commit e86d9dd
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,45 @@ public function export()
}
```

#### Using a specific View Builder

In some cases, it is better not to use the current model's View Builder `$this->viewBuilder` as any call to `$this->render()` will compromise any subsequent rendering.

For example, in the course of your current controller's action, if you need to render some data as CSV in order to simply save it into a file on the server.

Do not forget to add to your controller:

```php
use Cake\View\View;
use Cake\View\ViewBuilder;
```
So you can create a specific View Builder:

```php

// Your data array
$data = [];

// Params
$_serialize = 'data';
$_delimiter = ',';
$_enclosure = '"';
$_newline = '\r\n';

// Create the builder
$builder = new ViewBuilder;
$builder->layout = false;
$builder->setClassName('CsvView.Csv');

// Then the view
$view = $builder->build($data);
$view->set(compact('data', '_serialize', '_delimiter', '_enclosure', '_newline'));

// And Save the file
$file = new File('/full/path/to/file.csv', true, 0644);
$file->write($view->render());
```

## License

The MIT License (MIT)
Expand Down

0 comments on commit e86d9dd

Please sign in to comment.