Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » special layout without a page class. is it possible?

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

Page: 1 2
Go to End
Author Topic: 3590 Views
  • amir99
    Avatar
    Community Member
    5 Posts

    special layout without a page class. is it possible? Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: special layout without a page class. is it possible? Link to this post

    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.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: special layout without a page class. is it possible? Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: special layout without a page class. is it possible? Link to this post

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

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

    ...

    }

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: special layout without a page class. is it possible? Link to this post

    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!

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: special layout without a page class. is it possible? Link to this post

    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') );
    }
    }

  • sparkalow
    Avatar
    Community Member
    8 Posts

    Re: special layout without a page class. is it possible? Link to this post

    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.

  • Cuba
    Avatar
    Community Member
    12 Posts

    Re: special layout without a page class. is it possible? Link to this post

    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".

    3590 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.