跳转到主要内容
微笑的天使 提交于 2 July 2009

.info文件,它基本就是个用于保存主题设置信息的静态文本文件每一行都是“键=值”的形式(key=value),左边是key,右边是相应的value,中间用等号连接起来。在某行前面使用分号就可以将该行注释掉。使用方括号可以将多个value以数组的形式赋给一个键。

Drupal可以识别下面这些键的名称;对于那些非必要的没有在.info文件中明确定义出来的键,Drupal会使用默认值。

  • name(必要)
  • description(推荐)
  • screenshot
  • version(不推荐)
  • core(必要)
  • engine(多数情况下必要)
  • base theme
  • regions
  • features
  • stylesheets
  • scripts
  • php

抓紧时间来看看前面三个先。

name(必要)

当前主题的用户可读名称(human readable name),区别于机器可读名称(machine readable name),在拼写规则上没有什么限制,比如大小写、下划线一类的使用规则。

name = A fantasy name

description(推荐)

对主题的简短描述,会在站点后台的主题管理(Administer>Site building>themes)中呈现给使用者。

description = Tableless multi-column theme designed for blogs.

screenshot

该主题的演示缩略图,会在站点后台的主题管理中呈现给使用者,便于识别。如果没有特别指定,那么Drupal默认会在主题路径中寻找名为“screenshot.png”的图片。

所以呢,除非我们的缩略图文件名称不是“screenshot.png”,否则没有必要在.info文件中进行声明;或者如果我们的缩略图文件并不是存放在主题路径下,那么可以将指定路径声明给系统,比如:

screenshot = images/screenshot.png

version(不推荐)

版本号主要用在那些贡献给Drupal社区的主题模板里面而且每次更新时drupal.org会自动更新版本号,所以这事没什么意义;当然如果我们的主题并非用作贡献,那么使用版本号还是有好处的。形式很简单:

version = 1.0

core(必要)

从Drupal 6.x开始,所有模块及主题的.info文件中都必须声明可适应的Drupal核心版本。系统会自动将声明中的版本号与DRUPAL_CORE_COMPATIBILITY常量做比对,如果不相符,那么该模块或主题会被禁用。

core = 6.x

版本号的更新机制与前面说的被不推荐的version君相似,drupal.org会自动更新,确保用户下载的模块或主题可以适用于特定版本的核心。

engine(多数情况下必要)

主题引擎;多数Drupal主题使用phptemplate引擎,有那么一部分使用smarty;安装包自带的模板中就有那么一颗。

engine = phptemplate

base theme(基主题)

在之前子主题的话题中曾经有所了解;我们可以通过这个键使其他主题与当前主题建立父子关系,层级不限,无穷尽也;有关父子主题的继承机制也可以参考前面链接的文章。

需要注意的是在声明中要使用主题的机器可读名称(machine readable name)。

base theme = garland

regions(区域)

对于打造自定义主题很重要的一部分,用来声明可以容纳区块(block)的区域;默认主题中有left、right一类,形式可以参考。其中键的方 括号中需要使用区域的机器可读名称(left、right),对应的值就是用户可读名称(Left sidebar、Right sidebar),用来在后台管理的block设置中作为区域标题。

如果没有声明任何区域,那么系统会使用如下几个作为默认;可以随意篡改,也可自主添加,一切都看需求。

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

另外Drupal 7里面新增了一个默认区域:

regions[help] = Help

features

很多由主题输出的页面元素都可以在主题管理中设置显示状态(显示或隐藏),.info文件中的features键值就是用来控制哪些元素可以被这样设置的下面的代码示例列出了所有可以被控制的页面元素,其中primary_links和secondary_link被分号注释掉,这样在主题管理中我们就无法控制它们的显示状态了。

features[] = logo
features[] = name
features[] = slogan
features[] = mission
features[] = node_user_picture
features[] = comment_user_picture
features[] = search
features[] = favicon
; These last two disabled by redefining the
; above defaults with only the needed features.
; features[] = primary_links
; features[] = secondary_links

为什么放着控制权好好的不要,非要设置为无法控制呢?比如我们正在使用自己打造的主题,很多页面元素都是按照设计或功能需求来确定是否使用,没有必要通过在主题管理中再累赘的多出一个设置选项出来。

stylesheets(样式表)

用来声明当前主题使用到的样式表;可以通过这个键值来添加新样式表或覆盖父级同名样式表,在后面会专门有文档详解。

stylesheets[all][] = theStyle.css

在Drupal 6中,如果这里不做声明,那么系统会默认寻找使用style.css文件;Drupal 7改变了这个机制,如果不做声明,那么系统不会主动去使用默认样式文件。个人觉得这样比较好了,用就声明,不用就留空,省得还要专门为了覆盖来声明同名文件造成混淆。

scripts(脚本)

道理与stylesheets相同,用来声明脚本文件。

scripts[] = myscript.js

Drupal 6中,如果主题路径中存在名为script.js的文件,那么系统会自动调用作为主题的默认脚本,Drupal 7中取消了这个机制,同样是只有声明才会拿来用。

php

用来声明当前主题所支持的PHP的最低版本;多数情况下不需要做此声明。

php = 4.3.3

下面是默认主题Garland和它的子主题Minnelli各自的.info文件,我们可以对应着之前讲到的这些键值来参考围观:

Garland

; $Id: garland.info,v 1.5 2007/07/01 23:27:32 goba Exp $
name = Garland
description = Tableless, recolorable, multi-column, fluid width theme (default).
version = VERSION
core = 6.x
engine = phptemplate
stylesheets[all][] = style.css
stylesheets[print][] = print.css
 
; Information added by drupal.org packaging script on 2008-02-13
version = "6.0"
project = "drupal"
datestamp = "1202913006"

Minnelli

; $Id: minnelli.info,v 1.7 2007/12/04 20:58:44 goba Exp $
name = Minnelli
description = Tableless, recolorable, multi-column, fixed width theme.
version = VERSION
core = 6.x
base theme = garland
stylesheets[all][] = minnelli.css
 
; Information added by drupal.org packaging script on 2008-02-13
version = "6.0"
project = "drupal"
datestamp = "1202913006"

drupal-doc-theme-info-display

name = Garland description = Tableless, recolorable, multi-column, fluid width theme (default). version = VERSION core = 6.x engine = phptemplate stylesheets[all][] = style.css stylesheets[print][] = print.css ; Information added by drupal.org packaging script on 2008-02-13 version = "6.0" project = "drupal" datestamp = "1202913006"  

Minnelli sub-theme of Garland.:

name = Minnelli description = Tableless, recolorable, multi-column, fixed width theme. version = VERSION core = 6.x base theme = garland stylesheets[all][] = minnelli.css ; Information added by drupal.org packaging script on 2008-02-13 version = "6.0" project = "drupal" datestamp = "1202913006"

Note that everything from the line "; Information added by drupal.org packaging script on 2008-02-13" and down is added by the drupal.org packaging script. You should never manually add the project and datestamp keys. The version key added manually (in the first section) allows sites to use your theme when taken directly from git.