From 83fde137ef9cea8b038c37b5bf5ffce1503fb20b Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 26 Sep 2024 16:26:17 +1200 Subject: [PATCH] DOC Document changes to template layer --- .../01_Templates/01_Syntax.md | 24 ++++--------------- .../04_Preview.md | 21 ---------------- 2 files changed, 4 insertions(+), 41 deletions(-) diff --git a/en/02_Developer_Guides/01_Templates/01_Syntax.md b/en/02_Developer_Guides/01_Templates/01_Syntax.md index 9125c308..9cbdb033 100644 --- a/en/02_Developer_Guides/01_Templates/01_Syntax.md +++ b/en/02_Developer_Guides/01_Templates/01_Syntax.md @@ -565,26 +565,10 @@ 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 ``` @@ -592,11 +576,11 @@ You can also use the `$Me` variable, which outputs the current object in scope b 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 %> ``` diff --git a/en/02_Developer_Guides/15_Customising_the_Admin_Interface/04_Preview.md b/en/02_Developer_Guides/15_Customising_the_Admin_Interface/04_Preview.md index 183b7d4c..4e9cd0be 100644 --- a/en/02_Developer_Guides/15_Customising_the_Admin_Interface/04_Preview.md +++ b/en/02_Developer_Guides/15_Customising_the_Admin_Interface/04_Preview.md @@ -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