Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Multiple Templates for a Single Page

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

Page: 1
Go to End
Author Topic: 1823 Views
  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Multiple Templates for a Single Page Link to this post

    Hello,

    What would be the best way to have two templates for a single page? Practical use would be an e-mail newsletter archive where the public views the newsletters using one template and where the alternative template is used by an e-mail sending service when it pulls the newsletter to send it. The alternative template should be accessible by appending a query string to the page's URL.

    One idea on how to do this: in the newsletter page's controller, override something like handleAction() so that it changes the template used if the appropriate query string is present.

    What do you think of this approach? Have another idea?

    Sincerely,
    Ben

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Multiple Templates for a Single Page Link to this post

    Hi Ben

    That sounds about right. But AFAIK you don't have to override "handleAction".
    Let's assume you got 2 use-cases: Browsing the newsletter on the website and creating a page version to be sent by your mailing service.
    For the newsletter-browsing you would simply call the URL directly, i.e.:
    http://www.bengribaudo.com/my-newsletter

    To pull the "mail" version of the newsletter, you would call another URL, i.e.:
    http://www.bengribaudo.com/my-newsletter/mailable

    In your Newsletter_Controller you can then place a method called "mailable" that will be invoked when the latter URL is being called. There you would simply switch the template. Something like this:

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

    It would then render your newsletter with the "mailable.ss" Template.
    I hope that was what you were looking for.

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Multiple Templates for a Single Page Link to this post

    Thanks, Banal! Your tip helped us.

    Ben

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Multiple Templates for a Single Page Link to this post

    Hmm, this could also work for, say, an iPhone (or generic mobile) version of a site, or would you do that differently?

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