Skip to content

Instantly share code, notes, and snippets.

@camfindlay
Created April 14, 2014 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camfindlay/10688811 to your computer and use it in GitHub Desktop.
Save camfindlay/10688811 to your computer and use it in GitHub Desktop.
SilverStripe: Switching template in custom controller using GET variable.
<?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