Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC Document changes to template layer #591

Draft
wants to merge 1 commit into
base: 6
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading