In block controller.php set template

How can I in a controller of a block set the template the block is using. I’d like to switch between two templates, depending on a set of GET parameters when the page is called.

Is that possible? Or is it a bad idea because of caching in any case?

https://documentation.concretecms.org/api/9.1.1/Concrete/Core/Block/Block.html#method_setCustomTemplate

Thank you. I’ve tried this before as I saw it, but I am getting an “undefined function” error…

Found it. Very simple:

In a blocks controller.php view method, simply use:

$this->render(‘templates/yourtemplatefoldername/view’);

In case this is helpful for anyone in the future, using the render function in the view() function doesn’t include any custom .js or .css files in the template.

If you want to change to an alternate template, then you’ll want to go the route @enlil shared. In the controller, call $this->getBlockObject()->setCustomTemplate(‘templatename’); Note that this will reconfigure the block to use the alternate template always going forward.

If you simply want to use an alternate template for a single render, you’ll want to use Concrete\Core\Block\Block | ConcreteCMS API
e.g. $this->getBlockObject()->setTempFilename(‘templatename’);

1 Like