为区域指派内容

如果没有定义,Drupal 6中会做如下假设:

regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[header] = Header
regions[footer] = Footer

而在Drupal 7中增加了Highligted和Help作为缺省区域。Help区域的文字内容默认和Drupal 6中的page.tpl.php$help变量一致。侧边栏的机读名称也发生了变化。

regions[sidebar_first] = Left sidebar
regions[sidebar_second] = Right sidebar
regions[content] = Content
regions[header] = Header
regions[footer] = Footer
regions[highlighted] = Highlighted
regions[help] = Help

Drupal 7的bartik主题设置了如下的默认区域:

regions[header] = Header
regions[help] = Help
regions[page_top] = Page top
regions[page_bottom] = Page bottom
regions[highlighted] = Highlighted
regions[featured] = Featured
regions[content] = Content
regions[sidebar_first] = Sidebar first
regions[sidebar_second] = Sidebar second
regions[triptych_first] = Triptych first
regions[triptych_middle] = Triptych middle
regions[triptych_last] = Triptych last
regions[footer_firstcolumn] = Footer first column
regions[footer_secondcolumn] = Footer second column
regions[footer_thirdcolumn] = Footer third column
regions[footer_fourthcolumn] = Footer fourth column
regions[footer] = Footer

记住内部名称在page.tpl.php中会转换为区域变量。在上面的例子中,[header]区会以变量的形式输出指派到这一区域的所有内容,在Drupal 6中是$header,在Drupal 7中是$page[‘header’]。对于PHP变量的命名有一些限制,所以区域的机读名称也应该遵循同样地规定。基本上区域的内部名称只能包含数字、字母和下划线,且必须以字母开头。

而方括号之外供人阅读的名称用于在Administer > Site building > Blocks的配置页面中作为标签显示,在Drupal 7中这一路径为Administration > Structure > Blocks。

下图是Garland中的块管理表格:

administration table

Batrick中的块管理界面

batrick

注意事项:

  • 有专门用于渲染Block的模板文件

  • 新增任何自定义区域,都会使缺省区域失效。如果你想要在自定义区域之外还继续使用原有区域,需要手工添加。

  • 区域的定义顺序会反应到配置页面。

  • info文件的内容是缓存在数据库中的,所以对它的修改不会反应到Drupal中。(不要把info文件和主题注册表弄混了)。可以清理主题缓存

更新备注:

  • $footer_message区域在Drupal 7中已经被移除。

$content区域

Drupal 6和之前,page.tpl.php的$content变量的内容需要添加到Block里,并把Block放置在内容区域中(如果你定义了这个区域的话)。

在Drupal 7中,$content成为一个完整的区域,而且是主题中必须定义的内容。这一变化使得Drupal能在任何新主题中识别出放置主要页面内容的缺省区域。

在Drupal 6中,只能把Block添加这一区域中。要把block放倒主页内容之前,只能定义一个特别的区域。Drupal 7把主页内容做成了自己的Block。这样一来,无需新建新的区域,都可以把Block放置在内容前或是后都了。

手工为区域指定内容

注意,在#713462: drupal_add_region_content() not usable解决之前,Drupal 7和8都无法使用drupal_add_region_content,除非应用连接中的补丁。

要手工指定内容到某个区域,Drupal 6中可以使用drupal_set_content,Drupal 7中可以使用drupal_add_region_content,例如在Drupal 6中可以通过drupal_set_content('header', 'Welcome!')把这段文字添加到header区域中。

还有一个更有用的例子:把所有注释汇总到”right”区域中,把drop前缀换成你的主题机读名称即可。preprocessors is available提供了更多信息。

<?php
function drop_preprocess_comment(&$variables) {
    // Setup a few variables.
    $comment = $variables['comment'];
    $title = l(
    $comment->subject,
    comment_node_url(),
    array('fragment' => "comment-$comment->cid")
    );
    $new_marker = $comment->new ? t('new') : '';
    $by_line = t('by') .' '. theme('username', $comment);
    // Form the markup.
    $summary = '<div class="comment-sidebar">';
    $summary .= '<span class="title">' . "$title $new_marker</span>";
    $summary .= '<span class="credit">' . "$by_line</span>";
    $summary .= '</div>';
    // Set the comment into the right region.
    drupal_set_content('right', $summary);
}
?>

注意用这个方法设置内容会比block区域的读取要早,这一功能是通过template_preprocess_page > theme_blocks > drupal_get_content来实现的。

Avatar
崔秀龙

简单,是大师的责任;我们凡夫俗子,能做到清楚就很不容易了。

comments powered by Disqus
下一页
上一页

相关