【问题标题】:Displaying a drupal page without the template around it in Drupal 7在 Drupal 7 中显示没有模板的 drupal 页面
【发布时间】:2012-03-25 18:33:33
【问题描述】:

我想在 drupal 中呈现“基本页面”的内容。类似这样的问题:displaying a Drupal view without a page template around it 但对于 Drupal 7。

我的尝试几乎成功了:

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

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

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

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

【问题讨论】:

    标签: php drupal drupal-7 drupal-theming drupal-themes


    【解决方案1】:

    你快到了。您唯一需要做的就是添加一个自定义 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>
    
    • 刷新所有缓存。就是这样。

    【讨论】:

    • 列表项混淆了解析器。我从您的链接输入了代码,因为对于未来的人来说,没有什么比在死链接上找到正确的解决方案更糟糕的了:)。我会尽快尝试您的建议并回复您。
    • 请也试试 没有其他任何东西(因为这是文件的第一个版本;)。谢谢。
    • 不幸的是,这不起作用。我得到完全相同的输出。我验证了html--ajaxpage--ajax 模板都被使用(通过打印HTML cmets)。我认为的问题是这些块似乎是页面中$page['content'] 的一部分--ajax,所以无论如何它们都会被渲染。
    • 好吧,无论如何您需要自定义 html 模板来进行 ajax 响应。您是否使用任何基本主题框架(即 zen、omega 等)?
    • 你解决了吗?实际上,我认为您的块不会在 $page['content'] 中输出,也许只有某些模块会为您执行此操作...不知道我是否可以在没有完整项目的情况下以某种方式帮助您。跨度>
    【解决方案2】:

    根据 Ufonion Labs 的回答,我能够完全删除 Drupal 7中页面内容周围的所有HTML输出 实现hook_preprocess_pagehook_preprocess_html in 我的主题模板.php,像这样:

    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,我 获取带有页面内容的&lt;div&gt;,以便将其嵌入到另一个页面中。

    在这里无耻地采取了形式: displaying a Drupal view without a page template around it(drupal 7 的第二个最佳答案)。

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

    【讨论】:

      猜你喜欢
      • 2022-10-08
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多