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

How do I use renderWith() ???

Go to End

10 Posts   7515 Views


6 April 2011 at 12:42am Community Member, 6 Posts

I'm trying to create a module that uses several different templates depending on what is chosen,
(not going into great depth, but you get the idea)
Anyways, i need to be able to switch between these templates, so I tried figuring out how to use renderWith(); to do it.
But I can't figure it out, can anyone explain to me how to use it, I couldn't find it in the API either :P

Thanks :D


6 April 2011 at 7:30am Forum Moderator, 474 Posts

renderWith takes two arguments. The first is an array of templates to try and use, with the ones listed first getting looked for first. The second argument is an array that gets passed to customise().

renderWith returns a string that's the result of running the template.


6 April 2011 at 10:52am (Last edited: 6 April 2011 10:53am), Community Member, 36 Posts

I use renderWith() typically to tell a controller to display it's dataobjects on a specific SS file

Usually I just have this at the end of function index() or function init()

return $this->renderWith("NameOfTemplate");

'NameOftemplate' can be any SS file in my layout folder sans file extension

So have you tried this?

case 'template1': return $this->renderWith('template1');
case 'template2': return $this->renderWith("template2');

case 'template3': return $this->renderWith("template3');

default: return $this->renderWith('template1');


Don't forget to reload the page for any change in template to take effect


23 May 2011 at 7:37am (Last edited: 23 May 2011 7:38am), Community Member, 7 Posts

I'm having real problems with renderWith. I'm trying to use it within a controller's init() method to return a different version of the page if it is requested using ajax.

public function init() {


if (Director::is_ajax()) {
return $this->renderWith(array('AjaxImagesPage'));

But... when it renders (and I've checked debug_request and showtemplate to observe this) it seems to first spit it all out using AjaxImagesPage, but then override it all, change it's mind, and just go through the normal rendering sequence. The result is that the ajax-requested page is identical to the normal - not the goal!

I've tried to think of workarounds (a different controller method, or an api-style approach with totally different urls for the ajax calls), but I really need this functionality - a page site/mypage to render differently for standard and ajax requests.



23 May 2011 at 11:46am Forum Moderator, 5511 Posts

init() is called for *ever* action on the controller and also at the beginning. I believe the template may be reset in the index() action (which is the default action). Try moving that renderWith to the index action.


24 May 2011 at 1:22am Community Member, 7 Posts

Hey, thanks, that works when I do this:

public function index() {
if (Director::is_ajax()) {
return $this->renderWith('AjaxImagesPage');
else return $this->renderWith('Page', 'ImagesPage');

Is there any more general way to hand over to the default rendering chain than specifying 'Page', 'ImagesPage'?

Thanks again for your help :)


24 May 2011 at 8:01am Forum Moderator, 474 Posts

return array(); should do it.


24 May 2011 at 9:47pm Community Member, 7 Posts

Nice one, thanks. return array() works :)

Go to Top