Skip to content

Instantly share code, notes, and snippets.

@balupton
Created March 7, 2011 04:56
Show Gist options
  • Save balupton/858091 to your computer and use it in GitHub Desktop.
Save balupton/858091 to your computer and use it in GitHub Desktop.
Ajaxify a Website with Server Side Optimisations
<?php
// Our Page Action
public function pageAction ( ) {
// Prepare our variables for our view
// ...
// Handle our view
return $this->awesomeRender('page.html');
}
// Render Helper
public function awesomeRender ( $template ) {
// Get the full template path
$template_path = ...
// Render the template
$template_html = $this->view->render($template_path);
// Check for the XHR header
if ( IS_XHR ) {
// We are a AJAX Request, return just the template
$this->sendJson({'content':$template_html});
}
else {
// Wrap the Template HTML with the Layout and proceed as normal
// ...
}
// Done
}
@tezqa
Copy link

tezqa commented Apr 2, 2012

Hi,

I use this little piece of code to output necessary DOM elements for classic and ajax requests :

$ajaxRequest = false;
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') $ajaxRequest = true;
[...]

...

You example above seems to be more elegant, but could you explain how it works ?

@pomeh
Copy link

pomeh commented Apr 27, 2012

@tezqa I think its code is using the PHP Zend Framework, which use the MVC pattern (http://en.wikipedia.org/wiki/Model-view-controller). You can learn more about Zend here http://framework.zend.com/manual/en/learning.quickstart.intro.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment