21309 Posts in 5738 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php?
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 334 Views |
-
What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php?

10 October 2011 at 3:57pm
Hi,
I'm trying to get my head around the function extend()
What is the purpose of line 1255 in SiteTree.php:
public function MetaTags($includeTitle = true) {
$tags = "";
if($includeTitle === true || $includeTitle == 'true') {
$tags .= "<title>" . Convert::raw2xml(($this->MetaTitle)
? $this->MetaTitle
: $this->Title) . "</title>\n";
}$tags .= "<meta name=\"generator\" content=\"SilverStripe - http://silverstripe.org\" />\n";
$charset = ContentNegotiator::get_encoding();
$tags .= "<meta http-equiv=\"Content-type\" content=\"text/html; charset=$charset\" />\n";
if($this->MetaKeywords) {
$tags .= "<meta name=\"keywords\" content=\"" . Convert::raw2att($this->MetaKeywords) . "\" />\n";
}
if($this->MetaDescription) {
$tags .= "<meta name=\"description\" content=\"" . Convert::raw2att($this->MetaDescription) . "\" />\n";
}
if($this->ExtraMeta) {
$tags .= $this->ExtraMeta . "\n";
}$this->extend('MetaTags', $tags); // Results of this function are ignored... So what is the purpose of this line?
return $tags;
}Thanks very much.
VWD.
-
Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php?

10 October 2011 at 8:11pm
Well like the comment in the code says that line isn't actually being used.
Since $tags is a string the line would have to have been:$tags = $this->extend('MetaTags', $tags);
Generally though, anywhere you find $this->extend() is a chance for you to hook in with a decorator or extension and make changes or run your own code.
-
Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php?

11 October 2011 at 12:20am
The arguments are passed by reference so that you can modify the variable value. http://php.net/manual/en/language.references.php
-
Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php?

11 October 2011 at 6:47pm
So correct me if I'm wrong... if I create an extension/decorator for SiteTree and implement the MetaTags method, then the line of code there (line 1255 of SiteTree.php) will ensure that any time SiteTree->MetaTags(...) is called, my extension/decorator's MetaTag(...) method is also called?
So would that sum up the purpose of Object::extend(...)? It just ensures that the specified function will be called by all associated extensions/decorators if it is implemented by them?
Thanks guys. Just trying to see what it's there for.
Many thanks.
VWD. -
Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php?

11 October 2011 at 9:04pm
Yes that's pretty much correct. Just remember to make the variable a reference in your function definition.
Sofunction MetaTags($tags) {...
wouldn't actually do anything but
function MetaTags(&$tags) {...
would. As you can see the variable itself is not passed by reference in the code (and that's deprecated since 5.3) but if you make it a reference in your function definition it should all work out.
| 334 Views | ||
|
Page:
1
|
Go to Top |



