2

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?

Community
  • 1
  • 1
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180

2 Answers2

6

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.
htoip
  • 437
  • 5
  • 19
Alex Smirnoff
  • 478
  • 4
  • 10
  • The list item confused the parser. I entered the code from your link because there is nothing worse for future people than to find that the correct solution was here on a dead link :). I'll try your suggestion shortly and get back to you. – Tamás Szelei Mar 26 '12 at 15:24
  • Please also try just without anything else (because this was a first version of the file ;) . Thank you. – Alex Smirnoff Mar 26 '12 at 15:29
  • Unfortunately this didn't work. I get the exact same output. I verified that both the `html--ajax` and `page--ajax` template is used (by printing HTML comments). The problem I think is that the blocks appear to be part of `$page['content']` in page--ajax, so they get rendered anyway. – Tamás Szelei Mar 26 '12 at 17:43
  • Well anyway you need custom html template for ajax response. Do you use any base theme framework (i.e. zen, omega etc)? – Alex Smirnoff Mar 27 '12 at 09:38
  • Did you solve it? Actually I don't think your blocks output in $page['content'], maybe only if some module do this for you... Don't know if I can help you with this somehow without having a full project. – Alex Smirnoff Mar 28 '12 at 07:31
  • I couldn't solve it yet unfortunately. To be exact it is $page['content']['body'] I'm displaying. Interestingly, if I `print_r` the `$page['content']` array I get several items that have the content without blocks, but when I try to use them the blocks are added. – Tamás Szelei Mar 28 '12 at 07:39
  • Answer number three is correct however there is a mysterious tick mark at the end of line two which is not supposed to be there. –  Oct 31 '13 at 19:04
4

Based on the answer of Ufonion Labs I was able to completely remove all the HTML output around the page content in Drupal 7 by implementing both hook_preprocess_page and hook_preprocess_html in my themes template.php, like this:

function MY_THEME_preprocess_page(&$variables) {
  if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
    $variables['theme_hook_suggestions'][] = 'page__embed';
  }
}

function MY_THEME_preprocess_html(&$variables) {
  if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
    $variables['theme_hook_suggestions'][] = 'html__embed';
  }
}

Then I added two templates to my theme: html--embed.tpl.php:

<?php print $page; ?>

and page--embed.tpl.php:

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

Now when I open a node page, such as http://example.com/node/3, I see the complete page as usual, but when I add the response_type parameter, such as http://example.com/node/3?response_type=embed, I only get the <div> with the page contents so it can be embedded in another page.

Shamelessly taken form here : displaying a Drupal view without a page template around it (second best answer for drupal 7).

Alexei solution still use the page template wich is in charge of displaying blocks

Community
  • 1
  • 1
Thony
  • 2,300
  • 1
  • 20
  • 20