Warning: Undefined array key "HTTPS" in include() (line 29 of /opt/www/fdv2/drupal.stage/web/sites/default/settings.local.php).
include('/opt/www/fdv2/drupal.stage/web/sites/default/settings.local.php') (Line: 4)
require('/opt/www/fdv2/drupal.stage/web/sites/drupal001.net/settings.php') (Line: 145)
Drupal\Core\Site\Settings::initialize('/opt/www/fdv2/drupal.stage/web', 'sites/drupal001.net', Object) (Line: 1099)
Drupal\Core\DrupalKernel->initializeSettings(Object) (Line: 701)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Drupal7里面region变量输出到模版的问题 - Drupal大学 - Drupal问答论坛
首页 / 帖子
Drupal7里面region变量输出到模版的问题

首先在html.tpl.php里面,print的都是region的变量?可以直接在这里面print 其他自定义region的变量吗?

其次在page.tpl.php里面,print region变量格式如下:

print render($page['header'])

$page是否包含了所有region变量?有没有通常的套路?

Drupal 7的theme搞得有点乱,大家过来说两句。谢谢!

3个答案
YOYO
发布于:2014-07-08 17:29

你可以在template.php里面,自己写一个hook_preprocess_html和hook_preprocess_page,把里面的参数var_dump出来,就知道html和page两层模板中各能用什么变量了

发布于:2014-07-10 08:39

html.tpl里没有region变量,$page变量由page.tpl模板来组织内容,这里面跟D6的page.tpl就差不多了,可以输出所有区域变量。

YOYO
发布于:2014-07-11 17:01

谢谢@幽灵和@流云,受到启发,过来总结一下。

首先,根据@幽灵的说法,我到theme.inc里面查看了三个函数:

template_preprocess_html 、template_preprocess_page和template_process_html,preprocess_page里面有如下代码,获取了region变量:

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里面有的region产生的变量,该变量在$page数组里面,@流云说得很正确!

之后查了一下文档,Drupal7 默认有两个“隐藏”region,page_top和page_bottom,这两个region是不在block页面显示的,具体看下面的函数:

/**
 * Implements hook_system_info_alter().
 */
function system_system_info_alter(&$info, $file, $type) {
  // Remove page-top and page-bottom from the blocks UI since they are reserved for
  // modules to populate from outside the blocks system.
  if ($type == 'theme') {
    $info['regions_hidden'][] = 'page_top';
    $info['regions_hidden'][] = 'page_bottom';
  }
}


这两个变量如何输出,请看下面的函数:

function template_process_html(&$variables) {
  // Render page_top and page_bottom into top level variables.
  $variables['page_top'] = drupal_render($variables['page']['page_top']);
  $variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);
  // Place the rendered HTML for the page body into a top level variable.
  $variables['page']              = $variables['page']['#children'];
  $variables['page_bottom'] .= drupal_get_js('footer');

  $variables['head']    = drupal_get_html_head();
  $variables['css']     = drupal_add_css();
  $variables['styles']  = drupal_get_css();
  $variables['scripts'] = drupal_get_js();
}

看了这些,疑问大概都解决了,具体函数在那个文件可以搜一下(不在同一处)!

总结以上信息,供大家参考一下!