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.

Template Questions /

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

special layout without a page class. is it possible?


Go to End


9 Posts   8996 Views

Avatar
amir99

Community Member, 5 Posts

25 February 2009 at 5:22am

I was wondering whether its possible to create a specialized template (layout) without having to add a matching PageXXX.php file.
In some situations you don't need a custom page type, only a custom layout, and in such case adding a Page class and a Page_Controller class seem redundant.

thanks for any advice on the issue.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 February 2009 at 6:35am

I feel your pain. I can't tell you how many times I've had to create empty classes for MySpecialPage and MySpecialPage_Controller.

One thing that might help is if it's just layout related, you can probably do it with CSS. I always specify a class and ID for my body tag

<body class="$ClassName" id="$URLSegment">

That way you can target pages based on their URLSegment.

Of course, if it's content-related, you're stuck creating empty classes. I created a Builder module that streamlines creating the classes and templates, so you can just do /builder/MySpecialPage and it does all the work.

You can probably find it if you do a search, or I can post it back up here if you're interested.

Avatar
Hamish

Community Member, 712 Posts

25 February 2009 at 8:47am

Can't you use renderWith in your page controller to tell sapphire which templates to use?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 February 2009 at 8:59am

Edited: 25/02/2009 8:59am

Yeah, that's true. Super ugly, though.


switch($this->URLSegment)
{
case "foo" :
return $this->renderWith(array('Bar'));
break;

...

}

Avatar
MarcusDalgren

Community Member, 288 Posts

22 September 2009 at 10:53am

I too felt your pain and I am working on a solution!

I ran into the same issue on the last site I built and I have finally managed to decipher and affect which templates SilverStripe chooses early enough in the process to actually make a difference.

I'm still working out the kinks but when I'm finished you'll be able to define $allowed_templates on a class and then be able to choose between them in the CMS. This way you can assign templates to the classes that need them and have the rest functioning normally.

The solution also comes with a normal template choosing process that looks at the hierarchy and looks for templates that reflect that, so if you have a page of the Page class with a subpage of the MyPage class it will first look for Page_MyPage.ss before looking for MyPage.ss.

All this and more for only $39.95! :P

Avatar
rob.s

Community Member, 78 Posts

28 October 2009 at 8:26pm

Hi,

i got the same issue.

My solution:

My 3 Files
mysite/code/Homepage.php
themes/<theme>/Homepage.ss <- my Layout template
themes/<theme>/Layout/Homepage.ss <- my template

class Homepage extends Page {
    

    
}

class Homepage_Controller extends Page_Controller {
    

    function init(){
        parent::init();
        $this->renderWith( array('Homepage') );
    }
}

Avatar
sparkalow

Community Member, 8 Posts

4 November 2009 at 5:53pm

I'm currently experimenting with a TextField for a template filename added to my base page class. You can then access the saved filename and add it to the template "suggestions" array with renderWith.

Here is my index method on my page class:


public function index() {
		return $this->renderWith(array($this->CustomTemplateFileName, $this->ClassName, 'Page'));
}

This allows setting a template file name in the CMS; almost useful.

Avatar
Cuba

Community Member, 12 Posts

28 April 2010 at 10:00am

Edited: 24/05/2010 1:26am

What I found out is that you can actually create your own SSviewer that can output the Layout and Page data as ordinary. Here's an example:

function index(){
		
		$ssv=new SSViewer("Page");
		$ssv->setTemplateFile("Layout", "MyTemplateName");
		return $this->renderWith($ssv);
	}

This will render the main Page as usual but use the Layout-file named "MyTemplateName.ss".

Go to Top