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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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