Jump to:

23473 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » How do I use renderWith() ???

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

Page: 1 2
Go to End
Author Topic: 7353 Views
  • fengdeadshot
    Avatar
    Community Member
    6 Posts

    How do I use renderWith() ??? Link to this post

    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

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: How do I use renderWith() ??? Link to this post

    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.

  • BlueScreen
    Avatar
    Community Member
    36 Posts

    Re: How do I use renderWith() ??? Link to this post

    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

  • electronic-womble
    Avatar
    Community Member
    7 Posts

    Re: How do I use renderWith() ??? Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: How do I use renderWith() ??? Link to this post

    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.

  • electronic-womble
    Avatar
    Community Member
    7 Posts

    Re: How do I use renderWith() ??? Link to this post

    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

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: How do I use renderWith() ??? Link to this post

    return array(); should do it.

  • electronic-womble
    Avatar
    Community Member
    7 Posts

    Re: How do I use renderWith() ??? Link to this post

    Nice one, thanks. return array() works

    7353 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.