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 01:59

there are probably a number of ways around this, however, the "easiest" may be just setting your own custom theme, and having the page.tpl.php just be empty, or some random divs

// page.tpl.php
<div id="page"><?php print $content ?></div>

this method would basically just allow node.tpl.php to show (or any of drupal's form views, etc...) and would be an easy way to avoid modifying core, or having to alter the theme registry to avoid displaying page.tpl.php in the first place.

edit: see comments

ok i played around with views a bit, it looks like it takes over and constructs it's own "node.tpl.php" (in a sense) for display within "page.tpl.php". on first glance, my gut feeling would be to hook into theme_registry_alter().

when you're looking at a views page, you have access to piles of information here, as well as the page.tpl.php paths/files. as such i would do something like:

function modulejustforalteration_theme_registry_alter(&$variables) {
  if (isset($variables['views_ui_list_views']) ) {
  // not sure if that's the best index to test for "views" but i imagine it'll work
  // as well as others
    $variables['page']['template'] = 'override_page';        
  }
}

this should allow you to use a "override_page.tpl.php" template in your current theme in which you can remove anything you want (as my first answer above).

a few things:

  • as i said, not sure if views_ui_list_views is always available to check against, but it sounds like it should be set if we're looking at a view
  • you can alter the theme paths of the page array if you prefer (to change the location of where drupal will look for page.tpl.php, instead of renaming it altogether)
  • there doesn't appear to be any identifiers for this specific view, so this method might be an "all views will be stripped" approach. if you need to strip the page.tpl.php for a specific view only, perhaps hooking into template_preprocess_page() might be a better idea.
查看更多
做个烂人
3楼-- · 2019-01-30 02:01

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).

查看更多
祖国的老花朵
4楼-- · 2019-01-30 02:03

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

查看更多
该账号已被封号
5楼-- · 2019-01-30 02:04

jeroen's answer was what did for me after playing with it. I have a Drupal 7 site.

  1. First of all make sure you replace MY_THEME with your theme name. Yes it is obvious but most newbies miss this.

  2. I actually already had a function MY_THEME_preprocess_page(&$variables) {. Do not recreate the function then but add this code at the end of the function before you close it with }.


if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
  $variables['theme_hook_suggestions'][] = 'page__embed';
}
  1. My function used $vars not $variables, so I had to update that as well. Again obvious if you think look for it.
查看更多
欢心
6楼-- · 2019-01-30 02:07

Based on answer of Philadelphia Web Design (thanks) and some googling (http://drupal.org/node/957250) here is what worked for me in Drupal 7 to get chosen pages displayed without the template:

function pixture_reloaded_preprocess_page(&$vars)
{
  if ( isset($_GET['vlozeno']) && $_GET['vlozeno'] == 1 ) {
        $vars['theme_hook_suggestions'][] = 'page__vlozeno';
  }   
}

instead of phptemplate, in D7 there has to be the name_of_your_theme in the name of the function. Also, I had to put two underscores __ in the php variable with the file name, but the actual template file name needs two dashes --

content of page--vlozeno.tpl.php :

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

The output, however, still has got a lot of wrapping and theme's CSS references. Not sure how to output totally unthemed data...

查看更多
姐就是有狂的资本
7楼-- · 2019-01-30 02:08

There is also http://drupal.org/project/pagearray which is a general solution...

Also, @Scott Evernden's solution is a cross site scripting (XSS) security hole. Don't do that. Read the documentation on drupal.org about how to Handle Text in a Secure Fashion http://drupal.org/node/28984

查看更多
登录 后发表回答