21309 Posts in 5738 Topics by 2603 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 402 Views |
-
Render data with a different template

22 May 2012 at 2:21am
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 -
Re: Render data with a different template

22 May 2012 at 5:17pm
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.
-
Re: Render data with a different template

22 May 2012 at 7:31pm Last edited: 22 May 2012 11:26pm
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.
| 402 Views | ||
|
Page:
1
|
Go to Top |

