Skip to content

Commit

Permalink
DOC Document changes to template layer
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 26, 2024
1 parent 9ec893b commit 6792106
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 41 deletions.
24 changes: 4 additions & 20 deletions en/02_Developer_Guides/01_Templates/01_Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,38 +565,22 @@ refer directly to properties and methods of the [`Member`](api:SilverStripe\Secu
### `fortemplate()` and `$Me` {#fortemplate}

If you reference some `ModelData` object directly in a template, the `forTemplate()` method on that object will be called.
This can be used to provide a default template for an object.

```php
// app/src/PageType/HomePage.php
namespace App\PageType;

use Page;

class HomePage extends Page
{
public function forTemplate(): string
{
// We can also render a template here using $this->renderWith()
return 'Page: ' . $this->Title;
}
}
```
This can be used to provide a default template for an object. The default implementation renders the model using templates named after the class or its superclasses.

```ss
<%-- calls forTemplate() on the first page in the list and prints Page: Home --%>
<%-- calls forTemplate() on the first page in the list --%>
$Pages->First
```

You can also use the `$Me` variable, which outputs the current object in scope by calling `forTemplate()` on the object.
This is especially helpful when you want to directly render items in a list you're looping over.

> [!WARNING]
> If the object does not have a `forTemplate()` method implemented, this will throw an error.
> If the object does not have an appropriate template and doesn't override the `forTemplate()` method, this will throw an error.
```ss
<% loop $Pages %>
<%-- calls forTemplate() on the current object in scope and prints Page: Home --%>
<%-- calls forTemplate() on the current object in scope --%>
$Me
<% end_loop %>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,27 +272,6 @@ list of items and indirectly calling `forTemplate` using the [`$Me` template var
This method will be used by the `cmsPreview` action in the `MyAdmin` class to tell the
CMS what to display in the preview panel.

The `forTemplate` method will probably look something like this:

```php
namespace App\Model;

use SilverStripe\ORM\CMSPreviewable;
use SilverStripe\ORM\DataObject;

class Product extends DataObject implements CMSPreviewable
{
// ...

public function forTemplate(): string
{
// If the template for this DataObject is not an "Include" template, use the appropriate type here
// e.g. "Layout".
return $this->renderWith(['type' => 'Includes', self::class]);
}
}
```

#### The `ModelAdmin` implementation

We need to add the `cmsPreview` action to the `MyAdmin` class, which will output the
Expand Down

0 comments on commit 6792106

Please sign in to comment.