php 显示一个没有页面模板的 Drupal 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/247991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:03:39  来源:igfitidea点击:

displaying a Drupal view without a page template around it

phpdrupaldrupal-views

提问by ceejayoz

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.

我想显示一个没有通常围绕它的页面模板的 Drupal 视图 - 我只想要视图节点的纯 HTML 内容。

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

此视图将包含在另一个非 Drupal 站点中。

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.

我希望必须使用多个视图来执行此操作,因此可以让我快速轻松地设置这些视图的解决方案将是最好的 - 我不想每次都需要创建 .tpl.php 文件在某处包括一个视图。

回答by

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.

我一直在寻找一种通过 ajax 提取节点数据的方法,并为 Drupal 6 提出了以下解决方案。 实施以下更改后,如果您在 URL 中添加 ajax=1(例如 mysite.com/node/1?ajax= 1),你只会得到内容,没有页面布局。

in the template.php file for your theme:

在您的主题的 template.php 文件中:

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:

然后使用以下内容在您的主题目录中创建 page-ajax.tpl.php :

<?php print $content; ?>

回答by jeroen

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_pageand hook_preprocess_htmlin my themes template.php, like this:

根据 Ufonion Labs 的回答,通过在我的主题 template.phphook_preprocess_pagehook_preprocess_html我的主题 template.php 中实现,我能够完全删除 Drupal 7 中页面内容周围的所有 HTML 输出,如下所示:

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:

然后我在我的主题中添加了两个模板html--embed.tpl.php

<?php print $page; ?>

and page--embed.tpl.php:

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 onlyget the <div>with the page contents so it can be embedded in another page.

现在,当我打开一个节点页面时,例如http://example.com/node/3,我像往常一样看到完整的页面,但是当我添加 response_type 参数时,例如http://example.com/node/ 3?response_type=embed,我得到<div>页面内容,所以它可以嵌入到另一个页面中。

回答by SwarmIntelligence

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.

我知道这个问题已经得到回答,但我想添加我自己的解决方案,该解决方案使用费城网页设计 (PWD) 答案的元素并使用 Owen 建议的 hook_theme_registry_alter。使用此解决方案,您可以直接从自定义模块加载模板。

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:

首先,我将 raw.tpl.php 添加到模块内新创建的“模板”文件夹中。raw.tpl.php 的内容与 PWD 的 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:

接下来,我以与 PWD 相同的方式在我的模块中实现了 hook_preprocess_page(除了我修改了 $_GET 参数并更新了模板文件引用:

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

最后,我实现了 hook_theme_registry_alter 以将我的模块的“模板”目录添加到主题注册表(基于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.

现在,当我将 ?raw=1 添加到视图的 URL 路径时,它将在我的模块中使用指定的模板。

回答by rinogo

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!

干杯!

回答by Nabil Kadimi

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

使用exit();而不是return TRUE;(见评论)会好得多。

回答by Itangalo

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

1) 下载并安装 Views Bonus Pack ( http://drupal.org/project/views_bonus) 2) 创建一个视图显示“Feed”并使用样式“XML”(或您认为更适合您需要的东西)。3) 如果您对标准的 XML 输出不满意,可以通过调整视图的模板来更改它。检查“主题”设置以获取有关此特定视图的替代模板名称的建议(因此您仍将保留默认的 XML 输出以供将来使用)。

Good luck! //Johan Falk, NodeOne, Sweden

祝你好运!//Johan Falk,NodeOne,瑞典

回答by Ufonion Labs

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

基于Philadelphia Web Design的回答(感谢)和一些谷歌搜索(http://drupal.org/node/957250),这是在Drupal 7中对我有用的方法,可以在没有模板的情况下显示所选页面:

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

而不是 phptemplate,在 D7 中,函数名称中必须有 name_of_your_theme。另外,我必须在带有文件名的 php 变量中添加两个下划线 __,但实际的模板文件名需要两个破折号——

content of page--vlozeno.tpl.php :

页面内容--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...

然而,输出仍然有很多包装和主题的 CSS 引用。不知道如何输出完全没有主题的数据......

回答by Chris

Assuming you're in Drupal 6, the easiest way to do this is to put a phptemplate_views_view_unformatted_VIEWNAMEcall in template.php(assumes your view is unformatted - if it's something else, a list say, use the appropriate theme function). Theme the view results in this theme call then, instead of returning the results as you normally would, print them and return NULL. This will output the HTML directly.

假设您在 Drupal 6 中,最简单的方法是phptemplate_views_view_unformatted_VIEWNAME调用template.php(假设您的视图未格式化 - 如果它是其他内容,例如列表,请使用适当的主题功能)。然后在此主题调用中对视图结果进行主题化,而不是像通常那样返回结果,而是打印它们并返回NULL。这将直接输出 HTML。

PS - make sure to clear your cache (at /admin/settings/performance) to see this work.

PS - 确保清除缓存(在 /admin/settings/performance)以查看这项工作。

回答by Chris

I like the Drupal module. BUt, here's another way.

我喜欢 Drupal 模块。但是,这是另一种方式。

copy page.tpl.php in your theme folder to a new file called page-VIEWNAME.tpl.php, where VIEWNAME is the machine-readible name of the view.

将主题文件夹中的 page.tpl.php 复制到名为 page-VIEWNAME.tpl.php 的新文件中,其中 VIEWNAME 是视图的机器可读名称。

Then edit page-VIEWNAME.tpl.php to suit.

然后编辑 page-VIEWNAME.tpl.php 以适应。

回答by Owen

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

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

这种方法基本上只允许 node.tpl.php 显示(或任何 drupal 的表单视图等...),并且是一种避免修改核心或必须更改主题注册表以避免显示 page.tpl 的简单方法首先是 .php。

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

好吧,我玩了一下视图,看起来它接管并构建了它自己的“node.tpl.php”(在某种意义上)以在“page.tpl.php”中显示。乍一看,我的直觉是要钩进去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:

当您查看视图页面时,您可以在此处访问大量信息以及 page.tpl.php 路径/文件。因此,我会做类似的事情:

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

这应该允许您在当前主题中使用“override_page.tpl.php”模板,您可以在其中删除任何您想要的内容(作为我上面的第一个答案)。

a few things:

一些东西:

  • as i said, not sure if views_ui_list_viewsis 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 pathsof the pagearray 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.
  • 正如我所说,不确定是否views_ui_list_views总是可以检查,但如果我们正在查看视图,听起来应该设置它
  • 如果您愿意theme paths,您可以更改page数组的 的(更改 drupal 将查找 page.tpl.php 的位置,而不是完全重命名它)
  • 此特定视图似乎没有任何标识符,因此此方法可能是“所有视图都将被剥离”的方法。如果您只需要为特定视图剥离 page.tpl.php,也许挂钩template_preprocess_page()可能是一个更好的主意。