Jump to:

23372 Posts in 18175 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » Render data with a different template

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
Go to End
Author Topic: 1026 Views
  • johannes
    Avatar
    Community Member
    20 Posts

    Render data with a different template Link to this post

    Hi Guys,

    I try to speed up a bloated homepage and catch some content of tabs on demand only.

    Therefor I need to render the content of some tabs without loading all of the page, which is really slow because of CURL requests. I experimented with renderWith, it's working but it feels wrong.

    First I check for an action which I use to fetch the tab content:

    $params = $this->getURLParams();

    if ($params['Action']) {
    if ($params['Action'] == 'latestsamples') {
    echo $this->renderWith('LatestBooks');
    exit;
    }
    }

    Since I couldn't make the page stop rendering the HomePage Template I just print the returned string and shut it off.

    The code will do for now, but there are more tabs and I'd like to do it i a proper way. Can somebody help me out?

    Cheers,
    Johannes

  • sonet
    Avatar
    Community Member
    33 Posts

    Re: Render data with a different template Link to this post

    Maybe you can AJAX the tab requests... something along the lines of:

    Controller:

       function latestsamples() {
          if($this->isAjax) {
             return $this->renderWith('LatestBooks');
          }
          else {
             return Array();
          }
       }

       public function init() {
          parent::init();

          if(Director::is_ajax()) {
             $this->isAjax = true;
          }
          else {
             $this->isAjax = false;
          }

          Requirements::javascript('https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');

       }

       public function MyBooks() {
          ...
          return $books;
       }

    Templates:

    templates/Layout/HomePage.ss

          <div id="tab-container">
             <div id="ajaxTab">
                ...
             </div>
          </div>

          <a href="{$Link}latestsamples" onclick="$('#ajaxTab').load('$Link' + 'latestsamples'); return false;">
             Latest Books
          </a>

    templates/Includes/LatestBooks.ss

    <h2>Latest Books</h2>
    <% control MyBooks %>
          ...
    <% end_control %>

    I'm not sure how I got it to work without having JS enabled and then maybe it's better to use jQuery for the click binding.. but as a starting point maybe this will help.

  • johannes
    Avatar
    Community Member
    20 Posts

    Re: Render data with a different template Link to this post

    I just added the function latestsamples to the controller and it's working like a charm.

    Thank you for your effort, you helped me a lot.

    1026 Views
Page: 1
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.