Displaying a drupal page without the template around it in Drupal 7

Tamás Szelei picture Tamás Szelei · Mar 25, 2012 · Viewed 7k times · Source

I'd like to render the contents of a "basic page" in drupal. Something like this question: displaying a Drupal view without a page template around it but for Drupal 7.

My attempt almost works:

function mytheme_preprocess_page(&$variables, $hook) {
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
        $variables['theme_hook_suggestions'][] = 'page__ajax';
  }  
}

And have a file named page--ajax.tpl.php in the same directory where template.php lives:

<?php print $page['content']; ?>

The problem is that it still renders the menu and my two custom blocks from the sidebar. I only want the page content. What should I change?

Answer

Alex Smirnoff picture Alex Smirnoff · Mar 26, 2012

You are almost there. The only thing you need is to add a custom HTML wrapper template.

  • Add a function to template.php:
function THEMENAME_preprocess_html(&$variables, $hook) {
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
    $variables['theme_hook_suggestions'][] = 'html__ajax';
  }
}
  • Create a file named html--ajax.tpl.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
  "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <?php print $styles; ?>
  <?php print $scripts; ?>
</head>
<body class="<?php print $classes; ?>">
  <?php print $page_top; ?>
  <?php print $page; ?>
  <?php print $page_bottom; ?>
</body>
</html>
  • Flush all caches. That's all.