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 Oct 3, 2024
1 parent 2cc7091 commit 94d90fe
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 48 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
4 changes: 2 additions & 2 deletions en/02_Developer_Guides/01_Templates/09_Casting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. `<element>$Field.CDATA</element>` will ensure that the `<element>` body is safely escaped as a string.
- e.g. `<element>$renderField.CDATA</element>` will ensure that the `<element>` 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. `<div data-my-field="$MyField.HTMLATT"></div>`
- [`RAWURLATT()`](api:SilverStripe\ORM\FieldType\DBField::RAWURLATT()) - encodes strings for use in URLs via [`rawurlencode()`](https://www.php.net/manual/en/function.rawurlencode.php)
Expand Down
2 changes: 1 addition & 1 deletion en/02_Developer_Guides/03_Forms/03_Form_Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ $field->setTemplate('CustomTextField');
// Sets the template for the wrapper around the text field. i.e.
// '<div class="text">'
//
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SearchPage extends Page
</fieldset>
<div class="Actions">
<% loop $Actions %>$Field<% end_loop %>
<% loop $Actions %>$renderField<% end_loop %>
</div>
</form>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ Form template with custom tab navigation (trimmed down):
<div class="cms-content-fields center">
<fieldset>
<% loop Fields %>$FieldHolder<% end_loop %>
<% loop Fields %>$renderFieldHolder<% end_loop %>
</fieldset>
</div>
Expand All @@ -729,11 +729,11 @@ Tabset template without tab navigation (e.g. `CMSTabset.ss`)
<div $AttributesHTML>
<% loop Tabs %>
<% if Tabs %>
$FieldHolder
$renderFieldHolder
<% else %>
<div $AttributesHTML>
<% loop Fields %>
$FieldHolder
$renderFieldHolder
<% end_loop %>
</div>
<% end_if %>
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 94d90fe

Please sign in to comment.