displaying a Drupal view without a page template a

2019-01-30 01:36发布

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.

17条回答
叛逆
2楼-- · 2019-01-30 02:16

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.

查看更多
forever°为你锁心
3楼-- · 2019-01-30 02:20

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.

查看更多
Explosion°爆炸
4楼-- · 2019-01-30 02:21

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!

查看更多
放荡不羁爱自由
5楼-- · 2019-01-30 02:21

On D7 you can use menu_execute_active_handler

$build = menu_execute_active_handler('user', FALSE);
return render($build);
查看更多
不美不萌又怎样
6楼-- · 2019-01-30 02:24

I was looking for a way to pull node data via ajax and came up with the following solution for Drupal 6. After implementing the changes below, if you add ajax=1 in the URL (e.g. mysite.com/node/1?ajax=1), you'll get just the content and no page layout.

in the template.php file for your theme:

function phptemplate_preprocess_page(&$vars) {

  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
        $vars['template_file'] = 'page-ajax';
  }

}

then create page-ajax.tpl.php in your theme directory with this content:

<?php print $content; ?>
查看更多
登录 后发表回答