2

我想在 drupal 中呈现“基本页面”的内容。像这样的问题:显示一个 Drupal 视图,周围没有页面模板,但适用于 Drupal 7。

我的尝试几乎奏效:

function mytheme_preprocess_page(&$variables, $hook) {
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
        $variables['theme_hook_suggestions'][] = 'page__ajax';
  }  
}

page--ajax.tpl.php并在 template.php 所在的同一目录中命名一个文件:

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

问题是它仍然从侧边栏呈现菜单和我的两个自定义块。我只想要页面内容。我应该改变什么?

4

2 回答 2

6

你快到了。您唯一需要的是添加自定义 HTML 包装模板。

  • 将函数添加到template.php
function THEMENAME_preprocess_html(&$variables, $hook) {
  if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
    $variables['theme_hook_suggestions'][] = 'html__ajax';
  }
}
  • 创建一个名为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>
  • 刷新所有缓存。就这样。
于 2012-03-26T14:49:04.973 回答
4

根据 Ufonion Labs 的回答,我可以通过在我的主题 template.php 中实现这两者来完全删除 Drupal 7 中页面内容周围的所有 HTML 输出,如下所示hook_preprocess_pagehook_preprocess_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';
  }
}

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

<?php print $page; ?>

page--embed.tpl.php

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

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

在这里无耻地采取形式: 显示 Drupal 视图,周围没有页面模板(drupal 7 的第二个最佳答案)。

Alexei 解决方案仍然使用负责显示块的页面模板

于 2012-08-31T09:26:45.167 回答