<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>region | 伪架构师</title>
    <link>/tags/region/</link>
      <atom:link href="/tags/region/index.xml" rel="self" type="application/rss+xml" />
    <description>region</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>zh</language><lastBuildDate>Mon, 07 Jul 2014 09:29:20 +0800</lastBuildDate>
    <image>
      <url>/img/logo-wide.png</url>
      <title>region</title>
      <link>/tags/region/</link>
    </image>
    
    <item>
      <title>为区域指派内容</title>
      <link>/post/assign-content-for-regions/</link>
      <pubDate>Mon, 07 Jul 2014 09:29:20 +0800</pubDate>
      <guid>/post/assign-content-for-regions/</guid>
      <description>&lt;p&gt;如果没有定义，Drupal 6中会做如下假设：&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[header] = Header
regions[footer] = Footer
&lt;/code&gt;&lt;/pre&gt;

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

&lt;pre&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Drupal 7的bartik主题设置了如下的默认区域：&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;记住内部名称在page.tpl.php中会转换为区域变量。在上面的例子中，[header]区会以变量的形式输出指派到这一区域的所有内容，在Drupal 6中是$header，在Drupal 7中是$page[&amp;lsquo;header&amp;rsquo;]。对于&lt;a href=&#34;http://us3.php.net/variables&#34; target=&#34;_blank&#34;&gt;PHP变量&lt;/a&gt;的命名有一些限制，所以区域的机读名称也应该遵循同样地规定。基本上区域的内部名称只能包含数字、字母和下划线，且必须以字母开头。&lt;/p&gt;

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

&lt;p&gt;下图是Garland中的块管理表格：&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;images/block_config_garland.png&#34; alt=&#34;administration table&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Batrick中的块管理界面&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;images/block_config_batrick.png&#34; alt=&#34;batrick&#34; /&gt;&lt;/p&gt;

&lt;p&gt;注意事项：&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;有专门用于渲染Block的&lt;a href=&#34;https://www.drupal.org/node/190815#block-tpl&#34; target=&#34;_blank&#34;&gt;模板文件&lt;/a&gt;。&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;新增任何自定义区域，都会使缺省区域失效。如果你想要在自定义区域之外还继续使用原有区域，需要手工添加。&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;区域的定义顺序会反应到配置页面。&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;info文件的内容是缓存在数据库中的，所以对它的修改不会反应到Drupal中。(不要把info文件和&lt;a href=&#34;https://www.drupal.org/node/173880#theme-registry&#34; target=&#34;_blank&#34;&gt;主题注册表&lt;/a&gt;弄混了)。可以&lt;a href=&#34;http://drupal.org/node/337176&#34; target=&#34;_blank&#34;&gt;清理主题缓存&lt;/a&gt;。&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;更新备注：&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;$footer_message区域在Drupal 7中已经被移除。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;$content区域&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Drupal 6和之前，page.tpl.php的$content变量的内容需要添加到Block里，&lt;strong&gt;并把Block放置在内容区域中&lt;/strong&gt;（如果你定义了这个区域的话）。&lt;/p&gt;

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

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

&lt;p&gt;&lt;strong&gt;手工为区域指定内容&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;注意，在&lt;a href=&#34;https://www.drupal.org/node/713462&#34; target=&#34;_blank&#34;&gt;#713462: drupal_add_region_content() not usable&lt;/a&gt;解决之前，Drupal 7和8都无法使用drupal_add_region_content，除非应用连接中的补丁。&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;要手工指定内容到某个区域，Drupal 6中可以使用&lt;a href=&#34;http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_set_content/6&#34; target=&#34;_blank&#34;&gt;drupal_set_content&lt;/a&gt;，Drupal 7中可以使用&lt;a href=&#34;http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_region_content/7&#34; target=&#34;_blank&#34;&gt;drupal_add_region_content&lt;/a&gt;，例如在Drupal 6中可以通过&lt;code&gt;drupal_set_content(&#39;header&#39;, &#39;Welcome!&#39;)&lt;/code&gt;把这段文字添加到header区域中。&lt;/p&gt;

&lt;p&gt;还有一个更有用的例子：把所有注释汇总到&amp;rdquo;right&amp;rdquo;区域中，把drop前缀换成你的主题机读名称即可。&lt;a href=&#34;https://www.drupal.org/node/223430&#34; target=&#34;_blank&#34;&gt;preprocessors is available&lt;/a&gt;提供了更多信息。&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-php&#34;&gt;&amp;lt;?php
function drop_preprocess_comment(&amp;amp;$variables) {
    // Setup a few variables.
    $comment = $variables[&#39;comment&#39;];
    $title = l(
    $comment-&amp;gt;subject,
    comment_node_url(),
    array(&#39;fragment&#39; =&amp;gt; &amp;quot;comment-$comment-&amp;gt;cid&amp;quot;)
    );
    $new_marker = $comment-&amp;gt;new ? t(&#39;new&#39;) : &#39;&#39;;
    $by_line = t(&#39;by&#39;) .&#39; &#39;. theme(&#39;username&#39;, $comment);
    // Form the markup.
    $summary = &#39;&amp;lt;div class=&amp;quot;comment-sidebar&amp;quot;&amp;gt;&#39;;
    $summary .= &#39;&amp;lt;span class=&amp;quot;title&amp;quot;&amp;gt;&#39; . &amp;quot;$title $new_marker&amp;lt;/span&amp;gt;&amp;quot;;
    $summary .= &#39;&amp;lt;span class=&amp;quot;credit&amp;quot;&amp;gt;&#39; . &amp;quot;$by_line&amp;lt;/span&amp;gt;&amp;quot;;
    $summary .= &#39;&amp;lt;/div&amp;gt;&#39;;
    // Set the comment into the right region.
    drupal_set_content(&#39;right&#39;, $summary);
}
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;注意用这个方法设置内容会比block区域的读取要早，这一功能是通过&lt;a href=&#34;http://api.drupal.org/api/function/template_preprocess_page/6&#34; target=&#34;_blank&#34;&gt;template_preprocess_page&lt;/a&gt; &amp;gt; &lt;a href=&#34;http://api.drupal.org/api/function/theme_blocks/6&#34; target=&#34;_blank&#34;&gt;theme_blocks&lt;/a&gt; &amp;gt; &lt;a href=&#34;http://api.drupal.org/api/function/drupal_get_content/6&#34; target=&#34;_blank&#34;&gt;drupal_get_content&lt;/a&gt;来实现的。&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
