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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How do I use renderWith() ???


Go to End


10 Posts   18083 Views

Avatar
fengdeadshot

Community Member, 6 Posts

6 April 2011 at 12:42am

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

Avatar
(deleted)

Community Member, 473 Posts

6 April 2011 at 7:30am

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.

Avatar
BlueScreen

Community Member, 36 Posts

6 April 2011 at 10:52am

Edited: 06/04/2011 10:53am

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?

switch($template) 
{	
	
case 'template1': return $this->renderWith('template1');
break;		
					 
case 'template2': return $this->renderWith("template2');
break;

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

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

}

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

Avatar
electronic-womble

Community Member, 7 Posts

23 May 2011 at 7:37am

Edited: 23/05/2011 7:38am

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() {
    parent::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.

Thanks.

Avatar
Willr

Forum Moderator, 5523 Posts

23 May 2011 at 11:46am

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.

Avatar
electronic-womble

Community Member, 7 Posts

24 May 2011 at 1:22am

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

Avatar
(deleted)

Community Member, 473 Posts

24 May 2011 at 8:01am

return array(); should do it.

Avatar
electronic-womble

Community Member, 7 Posts

24 May 2011 at 9:47pm

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

Go to Top