Drupal – Render a page without page template for Ajax

Sometimes we may want to render a data without the theme page template. This is useful when we want to retrieve data through asynchronous call such as Ajax.

So let’s create a page content only .tpl.php in the theme folder called page-ajax.tpl.php.

<?php print $content; ?>

 

Then we add the following template_preprocess_page() function in the theme template.php.

...
function <theme>_preprocess_page(&$vars) {
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
    $vars['template_file'] = 'page-ajax';
  }
}
...

 

So now whenever you add the ?ajax=1 query string to the URL, the page-ajax.tpl.php will be applied. For example:

 

Done =)

Reference: StackOverflow – displaying a Drupal view without a page template around it

2 thoughts on “Drupal – Render a page without page template for Ajax”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.