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/01_Templates/09_Casting.md b/en/02_Developer_Guides/01_Templates/09_Casting.md index d39604bd..616ed5e3 100644 --- a/en/02_Developer_Guides/01_Templates/09_Casting.md +++ b/en/02_Developer_Guides/01_Templates/09_Casting.md @@ -106,9 +106,9 @@ All `DBField` instances share the following useful methods for formatting their - [`RAW()`](api:SilverStripe\ORM\FieldType\DBField::RAW()) - outputs the *raw* value into the template with no escaping. - [`XML()`](api:SilverStripe\ORM\FieldType\DBField::XML()) (and its alias [`HTML()`](api:SilverStripe\ORM\FieldType\DBField::HTML())) - encodes the value (using [`htmlspecialchars()`](https://www.php.net/manual/en/function.htmlspecialchars.php)) before outputting it. - [`CDATA`](api:SilverStripe\ORM\FieldType\DBField::CDATA()) - formats the value safely for insertion as a literal string in an XML file. - - e.g. `$Field.CDATA` will ensure that the `` body is safely escaped as a string. + - e.g. `$renderField.CDATA` will ensure that the `` body is safely escaped as a string. - [`JS()`](api:SilverStripe\ORM\FieldType\DBField::JS()) - ensures that text is properly escaped for use in JavaScript. - - e.g. `var fieldVal = '$Field.JS';` can be used in JavaScript defined in templates to encode values safely. + - e.g. `var fieldVal = '$renderField.JS';` can be used in JavaScript defined in templates to encode values safely. - [`ATT()`](api:SilverStripe\ORM\FieldType\DBField::ATT()) (and its alias [`HTMLATT()`](api:SilverStripe\ORM\FieldType\DBField::HTMLATT())) - formats the value appropriate for an HTML attribute string - e.g. `
` - [`RAWURLATT()`](api:SilverStripe\ORM\FieldType\DBField::RAWURLATT()) - encodes strings for use in URLs via [`rawurlencode()`](https://www.php.net/manual/en/function.rawurlencode.php) diff --git a/en/02_Developer_Guides/03_Forms/03_Form_Templates.md b/en/02_Developer_Guides/03_Forms/03_Form_Templates.md index 05feb467..ebede72f 100644 --- a/en/02_Developer_Guides/03_Forms/03_Form_Templates.md +++ b/en/02_Developer_Guides/03_Forms/03_Form_Templates.md @@ -46,7 +46,7 @@ $field->setTemplate('CustomTextField'); // Sets the template for the wrapper around the text field. i.e. // '
' // -// The actual FormField is rendered into the holder via the `$Field` +// The actual FormField is rendered into the holder via the `$renderField` // variable. // // setFieldHolder() is used in most `Form` instances and needs to output diff --git a/en/02_Developer_Guides/03_Forms/How_Tos/02_Lightweight_Form.md b/en/02_Developer_Guides/03_Forms/How_Tos/02_Lightweight_Form.md index 0e931e6d..fb6d6c75 100644 --- a/en/02_Developer_Guides/03_Forms/How_Tos/02_Lightweight_Form.md +++ b/en/02_Developer_Guides/03_Forms/How_Tos/02_Lightweight_Form.md @@ -55,7 +55,7 @@ class SearchPage extends Page
- <% loop $Actions %>$Field<% end_loop %> + <% loop $Actions %>$renderField<% end_loop %>
``` diff --git a/en/02_Developer_Guides/15_Customising_the_Admin_Interface/02_CMS_Architecture.md b/en/02_Developer_Guides/15_Customising_the_Admin_Interface/02_CMS_Architecture.md index 1f821b30..70ce99b2 100644 --- a/en/02_Developer_Guides/15_Customising_the_Admin_Interface/02_CMS_Architecture.md +++ b/en/02_Developer_Guides/15_Customising_the_Admin_Interface/02_CMS_Architecture.md @@ -716,7 +716,7 @@ Form template with custom tab navigation (trimmed down):
- <% loop Fields %>$FieldHolder<% end_loop %> + <% loop Fields %>$renderFieldHolder<% end_loop %>
@@ -729,11 +729,11 @@ Tabset template without tab navigation (e.g. `CMSTabset.ss`)
<% loop Tabs %> <% if Tabs %> - $FieldHolder + $renderFieldHolder <% else %>
<% loop Fields %> - $FieldHolder + $renderFieldHolder <% end_loop %>
<% end_if %> 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