Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Page Titles


Go to End


4 Posts   1094 Views

Avatar
JonShutt

Community Member, 244 Posts

26 July 2011 at 10:06am

Hi,

I've got a page type called 'products' and within this, i'm showing load of products. At the moment, each of these products are just using the same page title the main page

in the page controller I have something like:

if(Director::URLParam('Action')=='show'){
if($ID= Director::URLParam('ID')){
$filter = "`urlName` = '".$ID."'";
return DataObject::get('Product', $filter);
}
}

and what I want is the html page title to be like : {Product Name}, {Product Category}, {My Site Name}

but I've really no idea where I can define the title so it comes back into the html title tag...

Avatar
JonShutt

Community Member, 244 Posts

26 July 2011 at 10:07am

in fact, i need my page title to show almost exactly the same the page title on this page!

Avatar
martimiz

Forum Moderator, 1391 Posts

27 July 2011 at 3:12am

It depends on how your page title is set in the first place. In the original blackcandy Page.ss it uses the $MetaTitle property:

<title><% if MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> &raquo; $SiteConfig.Title</title>

So you could change that. The thing is: you need to prepare it before the template is parsed, and in order to do that you need to know the product details in advance. One way to do that is use the init() method. Simplified and not tested:

function init() {
	if ($product = getProduct()) {
		$this->MetaTitle = $product->Title; // or what you want it to be
	}
}


function getProduct() {
	...
	return DataObject::get_by_id('Product', $ID);
}

Note: you could store the product in a variable so you don't have to do the query twice...

Avatar
JonShutt

Community Member, 244 Posts

27 July 2011 at 8:05am

Hi martimiz,

That makes sense, I had tried setting the meta title - but now i see it needs setting before the title part is written out to the page.
I'll give that a go - it'll help a lot with my page SEO to get the product titles into the pages...

Cheers