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

One Page - Two Templates??


Go to End


4 Posts   1690 Views

Avatar
Joshuarr

Community Member, 37 Posts

28 April 2009 at 11:33am

Edited: 28/04/2009 11:33am

Hi,
I'm a graphic designer (read: not a programmer) and am running into a pretty simple problem. I need two different page layouts for two separate instances of Uncle Cheese's image gallery and am wondering what the best way to do this would be. Is there a way to do this without making a new page type?

Any help would be appreciated,
J

Avatar
Hamish

Community Member, 712 Posts

28 April 2009 at 1:19pm

You will need to get into the code a bit. The page controller can decide what template to use.

for example, if you do the following in your page controller:

...
function myaction() {
	return $this->renderWith(array('CustomTemplate', 'Page'));
}
...

Now when someone goes to someurl.com/mypage/myaction it will return the page in your other template.

The next question is, how do you pick the template? Is it based on a URL param, a URL segment or maybe a CMS setting?

Avatar
Joshuarr

Community Member, 37 Posts

28 April 2009 at 2:52pm

Edited: 28/04/2009 2:52pm

Thanks Hamish,
That's actually as far as I got.
Cheese's ImageGalleryPage.php has the following:

	public function init() 
	{

		parent::init();
		self::prototype2jquery();
		Requirements::css('themes/clockwork_image_gallery/css/ImageGallery.css');
		
		if(!isset($this->urlParams['Action'])) {
			if($this->SingleAlbumView()) {
				die($this->renderWith(array('ImageGalleryPage_album','Page')));
			}
		}
		else if($this->CurrentAlbum())
			$this->includeUI();

	}

I am guessing this is where I put some conditional to flop over to the other template. I kind of know how to figure out how to parse the url and do it that way, but it seems flimsy.

I'd prefer to do this via the cms but am clueless how. Do I replace 'ImageGalleryPage_album' with a variable that is set via the cms? Ugh.

Avatar
Hamish

Community Member, 712 Posts

28 April 2009 at 3:21pm

Ah ha. Ok, I've never used Gallery so I don't know what some of that code does.

If wanted to set the template from the CMS, here is one way you could do it. In the page class, add a template db field. Eg:

...
static $db = array(
	"UseAlternateTemplate" => "Boolean"
);
...

function getCMSFields() {
	$fields = parent::getCMSFields();
	...
	$fields->addFieldToTab('Root.Main', new CheckboxField('UseAlternateTemplate', 'Use Alternate Template'));
	...
	return $fields;	
}

This gives a checkbox in the CMS to 'use the alternate template'.

Then, modify your page controller:

public function init() {

	parent::init(); 
	self::prototype2jquery(); 
	Requirements::css('themes/clockwork_image_gallery/css/ImageGallery.css'); 
       
	if(!isset($this->urlParams['Action'])) { 
		if($this->SingleAlbumView()) { 
			if($this->UseAlternateTemplate) {
				return $this->renderWith(array('MyAlternateTemplate', 'Page'));
			} else {
				// note that I've changed 'die' to return - should be the correct way I think.
				return $this->renderWith(array('ImageGalleryPage_album','Page')); 
			}
		} 
	} else if($this->CurrentAlbum()) 
		$this->includeUI();
}

None of the above is tested, but hopefully it helps.