displaying a Drupal view without a page template around it

49,680

Solution 1

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.

Solution 2

I know this question has already been answered, but I wanted to add my own solution which uses elements of Philadelphia Web Design's (PWD) answer and uses hook_theme_registry_alter, as suggested by Owen. Using this solution, you can load the template directly from a custom module.

First, I added raw.tpl.php to a newly created 'templates' folder inside my module. The contents of raw.tpl.php are identical to PWD's page-ajax.tpl.php:

<?php print $content; ?>

Next, I implemented hook_preprocess_page in my module in the same fashion as PWD (except that I modified the $_GET parameter and updated the template file reference:

function MY_MODULE_NAME_preprocess_page(&$vars) {
    if ( isset($_GET['raw']) && $_GET['raw'] == 1 ) {
        $vars['template_file'] = 'raw';
    }
} 

Finally, I implemented hook_theme_registry_alter to add my module's 'templates' directory to the theme registry (based on http://drupal.org/node/1105922#comment-4265700):

function MY_MODULE_NAME_theme_registry_alter(&$theme_registry) {
   $modulepath = drupal_get_path('module','MY_MODULE_NAME');
   array_unshift($theme_registry['page']['theme paths'], $modulepath.'/templates');
}

Now, when I add ?raw=1 to the view's URL path, it will use the specified template inside my module.

Solution 3

For others who may hit this page, if you're just working with standard callbacks (not necessarily views), this is easy. In your callback function, instead of returning the code to render within the page, use the 'print' function.

For example:

function mymodule_do_ajax($node)
{
    $rval = <<<RVAL
        <table>
            <th>
                <td>Data</td>
                <td>Data</td>
                <td>Data</td>
            </th>
            <tr>
                <td>Cool</td>
                <td>Cool</td>
                <td>Cool</td>
            </tr>
        </table>
RVAL;

    //return $rval; Nope!  Will render via the templating engine.
    print $rval; //Much better.  No wrapper.
}

Cheers!

Solution 4

Another way to do it which I find very handy is to add a menu item with a page callback function that doesn't return a string:

Example:

/**
 * Implementation of hook_menu.
 */
function test_menu(){
  $items['test'] = array (
    /* [...] */ 
    'page callback' => 'test_callback',
    /* [...] */ 
  );
  return $items;
}

function test_callback() {
  // echo or print whatever you want
  // embed views if you want
  // DO NOT RETURN A STRING
  return TRUE;
}    

-- Update

It would be much better to use exit(); instead of return TRUE; (see comment).

Solution 5

Hey, here's yet another way of doing it:

1) Download and install Views Bonus Pack (http://drupal.org/project/views_bonus) 2) Create a Views display "Feed" and use style "XML" (or something you think fits your needs better). 3) If you're not satisfied with the standard XML output, you can change it by adjusting the template for the view. Check the "theme" settings to get suggestions for alternative template names for this specific view (so you'll still have the default XML output left for future use).

Good luck! //Johan Falk, NodeOne, Sweden

Share:
49,680
ceejayoz
Author by

ceejayoz

CTO at Multibrain. Formerly Lead Developer GripMedia and the Democrat and Chronicle. Husband to one and father to two. PHP lover. LinkedIn: http://www.linkedin.com/in/ceejayoz Twitter: https://twitter.com/ceejayoz E-mail: chris (at) sternal (dash) johnson (dot) com.

Updated on August 03, 2020

Comments

  • ceejayoz
    ceejayoz over 3 years

    I would like to display a Drupal view without the page template that normally surrounds it - I want just the plain HTML content of the view's nodes.

    This view would be included in another, non-Drupal site.

    I expect to have to do this with a number of views, so a solution that lets me set these up rapidly and easily would be the best - I'd prefer not to have to create a .tpl.php file every time I need to include a view somewhere.