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.

General Questions /

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

Render Custom template


Go to End
Reply


12 Posts   1766 Views

Avatar
RD

Community Member, 6 Posts

9 April 2014 at 9:21am

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

Avatar
camfindlay

Forum Moderator, 185 Posts

9 April 2014 at 6:25pm

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.

Avatar
RD

Community Member, 6 Posts

10 April 2014 at 11:53am

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.

Avatar
camfindlay

Forum Moderator, 185 Posts

10 April 2014 at 4:15pm

Edited: 10/04/2014 5:40pm

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

Avatar
RD

Community Member, 6 Posts

14 April 2014 at 12:16pm

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

Avatar
camfindlay

Forum Moderator, 185 Posts

14 April 2014 at 12:26pm

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.

Avatar
moniuch

Community Member, 9 Posts

14 April 2014 at 7:29pm

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

Avatar
camfindlay

Forum Moderator, 185 Posts

15 April 2014 at 11:04am

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

Go to Top