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.

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

Static URLSegment?


Go to End
Reply


2 Posts   997 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

7 May 2009 at 6:58am

Is there a way to have a static URLSegment for a given page? Sometimes I have to hardcode URLSegments into my template, and, although it's unlikely, it would be nice if I could be sure the client wouldn't change the title and mess things up.

Avatar
banal

Community Member, 901 Posts

7 May 2009 at 9:53am

Edited: 07/05/2009 9:54am

Hi UncleCheese.
Well. You could use the Page ID, since that's immutable by your client. But this would fail if the client deletes the page and tries to re-create it.
In the getCMSFields function you could make the ID visible for the user inside the CMS:

$fields->addFieldToTab(
	'Root.Content.Main', 
	new ReadonlyField('PageId', 'Page ID', $this->ID)
);

And in the template you would do something like:

<% if ID = 3 %>
 .. specific content for page with id 3 here ...
<% end_if %>

Another alternative could be a field that can only be written once.
Here's an example how this could be done:

public static $db = array(
	'MyIDField' => 'Varchar(64)'
);

public function getCMSFields() {
	$fields = parent::getCMSFields();
	if($this->MyIDField == null){
		$fields->addFieldToTab(
			'Root.Content.Main', 
			new TextField('MyIDField', 'Page-ID')
		);
	} else {
		$fields->addFieldToTab(
			'Root.Content.Main', 
			new ReadonlyField('MyIDField', 'Page-ID')
		);
	}
	return $fields;
}