Jump to:

23487 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Render Custom template

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 2
Go to End
Author Topic: 1195 Views
  • RD
    Avatar
    Community Member
    5 Posts

    Render Custom template Link to this post

    Hi there,

    I am trying to serve a custom template based on the URl. Lets say i have the following URL and Templates.

    URL 1 = http://example.com/controller/?version=1
    URL 2 = http://example.com/controller/?version=2

    Template1 = ShowCase1.ss
    Template2 = ShowCase2.ss

    For URL1 i would like to render the page with template ShowCase1 and for URL2, I would like to render the page with template ShowCase2

    So, irrespective of the URL pattern which may have Action/ID/OtherId, If the request has get parameter called "Version" i have to provide that version of the custom template.

    So can anyone please help with solve this...really appreciate your help.

    Cheers
    RD

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: Render Custom template Link to this post

    should be able to use $this->request->getVar('version') in your controller to get the value. You could them look to use this in a switch statement to change the template rendering. You should also check to see if the variable exists before running through the switch and have a default template to render if nothing is supplied.

    $version = $this->request->getVar('version');
    if(isset($version)) {
    switch($this->request->getVar('version')){
    case 1:
    return $this->renderWith('MyTemplate');
    break;
    case 2:

    . . .etc

    }
    }

    Or something along those lines.

  • RD
    Avatar
    Community Member
    5 Posts

    Re: Render Custom template Link to this post

    thanks for the reply camfindlay, sorry i should have framed my question properly. I have got the logic sorted. But i am struggling to understand where should i add this code to if i want this logic to work at a global leel.

    I would like to get the process working on every controller available. I have already tried it on one of the test controllers init() function and it works fine. Not sure if this is the right place tough

    so I need to add this at a certain place in the control flow of the request that gets applied to any controller available.

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: Render Custom template Link to this post

    Do you mean applied to every controller or do you mean every action inside a controller?

    The action is what is rendered to a view, unless you perhaps set the variable in the ini() as a class variable like $this->template and them later in your actions use the variable to set the $this->renderWith().

    Might be good to get more context around what you are doing. Can you post an example of your controller class? Use http://sspaste.com/ if it is a long example.

    Here is a working example: http://sspaste.com/paste/show/53462bbdd8201

  • RD
    Avatar
    Community Member
    5 Posts

    Re: Render Custom template Link to this post

    Basically i am trying to do the split testing working for my site. the issue is there are various page types in my site that are not listed in CMS so i cannot create a version of these un-listed pages. Lets say these page are called UnlistedA, UnlistedB and UnlistedC

    These unlisted page types have actions within. So the idea was irrespective of the pagetype and action within the user is accessing if the URL has ?version=1 or 2 or 3 ....i have to render the data with that version of the template.

    I did not want to repeat the checking of version code in every Controller. So was looking for a place so that it gets applied to every controller and every action within the controller.

    Hope i managed to explain the scenario

    RD

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: Render Custom template Link to this post

    Did you check out the code I linked to about?

    If you are talking about Page_Controller you could take my code and put in the Page_Controller init function.

  • moniuch
    Avatar
    Community Member
    9 Posts

    Re: Render Custom template Link to this post

    FYI, sspaste.com is dead with "Bandwidth Limit Exceeded"

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: Render Custom template Link to this post

    That's no good ~ in that case I have made a gist for you to look at.

    https://gist.github.com/camfindlay/10688811

    This is a custom ContentController but if you are referring to Page_Controller init you could most likely adapt it (I don't know the details of your project).

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