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.

General Questions /

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

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

Render data with a different template


Go to End
Reply


3 Posts   1484 Views

Avatar
johannes

Community Member, 20 Posts

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

Avatar
sonet

Community Member, 33 Posts

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.

Avatar
johannes

Community Member, 20 Posts

22 May 2012 at 7:31pm

Edited: 22/05/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.