Created
April 14, 2014 23:02
-
-
Save camfindlay/10688811 to your computer and use it in GitHub Desktop.
SilverStripe: Switching template in custom controller using GET variable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyController extends ContentController { | |
private static $allowed_actions = array( | |
'myaction', | |
'myotheraction' | |
); | |
//A deafult template named the same as the controller as a fall back. | |
private static $template = "MyController"; | |
public function init(){ | |
parent::init(); | |
$version = $this->request->getVar('version'); | |
if(isset($version)) { | |
switch($this->request->getVar('version')){ | |
case 1: | |
//set the template using SilverStripe 3 config system. It changes tha value of the static. | |
$this->config()->template = "ShowCase1"; | |
break; | |
case 2: | |
$this->config()->template = "ShowCase2"; | |
break; | |
} | |
} | |
} | |
public function myaction(SS_HTTPRequest $request){ | |
$data = array( | |
"Title" => "Pass variable to template", | |
"Content" => $this->config()->template //output template for debug | |
); | |
//use the config system to get the value of the template. Note, 'Page' is the fallback template. | |
return $this->renderWith(array($this->config()->template, "Page"),$data); | |
} | |
public function myotheraction(SS_HTTPRequest $request){ | |
//should be available in all actions on a controller. | |
return $this->renderWith(array($this->config()->template, "Page")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment