From 0cbc02add1e4d4af1c331edcc5ab72c466e18a1e Mon Sep 17 00:00:00 2001 From: Jonathon Menz Date: Fri, 1 Feb 2019 09:33:20 -0800 Subject: [PATCH] DOCS Added 'Modifying Meta Tags' entry --- .../01_Templates/02_Common_Variables.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md b/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md index 9f0553663..6bebe8cb2 100644 --- a/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md +++ b/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md @@ -143,6 +143,27 @@ $MetaTags(false) $Title - Bob's Fantasy Football ``` +### Modifying Meta Tags + +You can override the `MetaComponents()` method on your `SiteTree` sub-classes or make use of the `MetaComponents` extension point to manipulate the underlying data that is rendered by `$MetaTags`. Example (for `Page` class): + +```php +public function MetaComponents() +{ + $tags = parent::MetaComponents(); + // Override the content of the Title tag (needs to be html) + if ($this->MetaTitle) { + $tags['title']['content'] = $this->obj('MetaTitle')->forTemplate(); + } + // Provide a default Meta Description + if (!$tags['description']['attributes']['content']) { + // provide raw text as attributes will be escaped later + $tags['description']['attributes']['content'] = $this->dbObject('Content')->LimitCharactersToClosestWord(300); + } + return $tags; +} +``` + ## Links ```ss