Update 04_Rendering_Templates.md

The method of including a Layout template into a full template file isn't documented, and isn't intuitive especially if you have come from the SS3 way of doing it via $this->customise($data)->renderWith(['MyCustomTemplate', 'Page']);  Adding this as an example will help people add layout templates to Controller actions, and form responses where a custom layout needs to inherit it's parent template.
This commit is contained in:
scttw 2020-07-08 16:15:32 +10:00 committed by GitHub
parent 162e4b7ff8
commit 34f5004b26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,6 +34,19 @@ echo $arrayData->renderWith('Coach_Message');
// returns "<strong>John</strong> is the Head Coach on our team."
```
If your template is a Layout template that needs to be rendered into the main Page template (to include a header and footer, for example), you need to render your Layout template into a string, and pass that as the Layout parameter to the Page template.
```php
$data = [
'Title' => 'Message from the Head Coach'
];
return $this->customise([
'Layout' => $this
->customise($data)
->renderWith(['Template\Path\From\templates\Layout\Coach_Message'])
])->renderWith(['Page']);
```
[info]
@ -107,4 +120,4 @@ class PageController extends ContentController
* [Controller actions/DataObjects as pages](https://www.silverstripe.org/learn/lessons/v4/controller-actions-dataobjects-as-pages-1)
* [AJAX behaviour and ViewableData](https://www.silverstripe.org/learn/lessons/v4/ajax-behaviour-and-viewabledata-1)
* [Dealing with arbitrary template data](https://www.silverstripe.org/learn/lessons/v4/dealing-with-arbitrary-template-data-1)
* [Creating filtered views](https://www.silverstripe.org/learn/lessons/v4/creating-filtered-views-1)
* [Creating filtered views](https://www.silverstripe.org/learn/lessons/v4/creating-filtered-views-1)