gpt4 book ai didi

php - 警告 : Cannot use a scalar value as an array in include()

转载 作者:行者123 更新时间:2023-12-03 23:05:48 25 4
gpt4 key购买 nike

为了在我的站点中设置特定页面的主题,我创建了一个名为 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 文件中写入文本并且它显示良好(而不是默认页面内容),但我根本无法在区域内呈现 block 。如果我将一个 block 分配给 schools_landing block ,我在页面上看不到任何内容。

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

最佳答案

node template 中, $page 是 bool 值,不是数组。这就是您收到该错误的原因。 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 描述为:

Regions:

  • $page['help']: Dynamic help text, mostly for admin pages.
  • $page['highlighted']: Items for the highlighted content region.
  • $page['content']: The main content of the current page.
  • $page['sidebar_first']: Items for the first sidebar.
  • $page['sidebar_second']: Items for the second sidebar.
  • $page['header']: Items for the header region.
  • $page['footer']: Items for the footer region.

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

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

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

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

关于php - 警告 : Cannot use a scalar value as an array in include(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com