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.

Template Questions

special layout without a page class. is it possible?


Go to End
Reply

9 Posts   3822 Views

Avatar
amir99

25 February 2009 at 5:22am Community Member, 5 Posts

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

25 February 2009 at 6:35am 4085 Posts

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

25 February 2009 at 8:47am Community Member, 712 Posts

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

Avatar
UncleCheese

25 February 2009 at 8:59am (Last edited: 25 February 2009 8:59am), 4085 Posts

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

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

...

}

Avatar
MarcusDalgren

22 September 2009 at 10:53am Community Member, 288 Posts

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

28 October 2009 at 8:26pm Community Member, 78 Posts

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

4 November 2009 at 5:53pm Community Member, 8 Posts

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

28 April 2010 at 10:00am (Last edited: 24 May 2010 1:26am), Community Member, 12 Posts

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