【问题标题】:Warning: Cannot use a scalar value as an array in include()警告:不能在 include() 中使用标量值作为数组
【发布时间】:2012-12-10 16:45:40
【问题描述】:

为了在我的站点中设置特定页面的主题,我创建了一个名为 node--2.tpl.php 的文件。根据我阅读的其他一些教程,我将其添加到我的 template.php 文件中:

function mtheme_preprocess_node(&$vars) {
  if (request_path() == 'node/2') {
    $vars['theme_hook_suggestions'][] = 'node__2';
  }
}

在此页面上,我希望渲染名为schools_landing 的区域。因此,node--2.tpl.php 看起来像这样,仅此而已:

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

这样做之后,我开始在管理员覆盖的顶部看到以下错误消息:

Warning: Cannot use a scalar value as an array in include() (line 1 of /home/something/public_html/project/sites/all/themes/mtheme/node--2.tpl.php).

此外,我可以在 node--2.tpl.php 文件中写入文本并且它显示良好(而不是默认页面内容),但我根本无法在该区域内渲染块。如果我为schools_landing 块分配一个块,我在页面上什么也看不到。

  1. 这是在特定页面上定义自定义内容的正确流程吗?
  2. 如何修复导致标量值作为数组错误消息的错误?
  3. 如何让我的块开始在该区域中渲染?

【问题讨论】:

    标签: php drupal drupal-7 drupal-theming


    【解决方案1】:

    node template 中,$page 是一个布尔值,而不是一个数组。这就是您收到该错误的原因。
    template_preprocess_node() 使用以下代码设置它。

    $variables['page']      = $variables['view_mode'] == 'full' && node_is_page($node);
    

    hook_preprocess_page() 使用您期望的值获取变量 $page
    template_preprocess_page() 包含以下代码。

      foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
        if (!isset($variables['page'][$region_key])) {
          $variables['page'][$region_key] = array();
        }
      }
    

    page.tpl.php$page 描述为:

    地区:

    • $page['help']:动态帮助文本,主要用于管理页面。
    • $page['highlighted']:突出显示的内容区域的项目。
    • $page['content']:当前页面的主要内容。
    • $page['sidebar_first']:第一个侧边栏的项目。
    • $page['sidebar_second']:第二个侧边栏的项目。
    • $page['header']:标题区域的项目。
    • $page['footer']:页脚区域的项目。

    可以从主题中实现额外的区域。

    作为旁注,template_preprocess_node() 已经建议了以下模板名称。

      $variables['theme_hook_suggestions'][] = 'node__' . $node->type;
      $variables['theme_hook_suggestions'][] = 'node__' . $node->nid;
    

    无需为您的主题或自定义模块推荐它们。

    【讨论】:

    • 好的,你的旁注很有道理。我删除了我的建议,因为它没有必要。其余的,我应该使用页面模板而不是节点模板吗?也就是说,如果我想在特定页面上呈现schools_landing 区域,我应该创建一个页面模板来执行此操作吗?
    猜你喜欢
    • 2014-02-26
    • 2011-08-26
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    相关资源
    最近更新 更多