21278 Posts in 5728 Topics by 2599 members
| Go to End | Next > | |
| Author | Topic: | 4126 Views |
-
How do I use renderWith() ???

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
Thanks
-
Re: How do I use renderWith() ???

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.
-
Re: How do I use renderWith() ???

6 April 2011 at 10:52am Last edited: 6 April 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
-
Re: How do I use renderWith() ???

23 May 2011 at 7:37am Last edited: 23 May 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.
-
Re: How do I use renderWith() ???

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.
-
Re: How do I use renderWith() ???

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
| 4126 Views | ||
| Go to Top | Next > |



