Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » One Page - Two Templates??

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1114 Views
  • Joshuarr
    Avatar
    Community Member
    37 Posts

    One Page - Two Templates?? Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: One Page - Two Templates?? Link to this post

    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?

  • Joshuarr
    Avatar
    Community Member
    37 Posts

    Re: One Page - Two Templates?? Link to this post

    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.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: One Page - Two Templates?? Link to this post

    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.

    1114 Views
Page: 1
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.