为主题编写info文件

.info文件是一个静态文本文件,用于主题的定义和配置。info文件的每一行都由一个键值对构成,例如name = my_theme。分号用来表明注释。有些键使用一点特别的语法:用中括号把一组相关内容组合为一个列表(类似数组)。如果你对数组不了解,可以看看Drupal核心主题中的.info文件,阅读其中的解释。即使.info文件并没有一个典型的缺省编辑工具,可以在Mac上使用TextEdit或者在Windows中使用Notepad来查看、编辑或者修改。

注意本节讲解的.info文件是Drupal主题相关的,而不是模块中使用的info文件。可以在模块开发指南编写info文件学习模块中info文件的写法。

举例

下面的例子是一个Drupal 6中Garland主题的.info文件

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

##主题命名规则

名字应该以字母开头,可以包含数字和下划线,不应含有连字符、空格以及标点。这个名字会被Drupal以PHP函数的方式来使用,所以需要遵循同样的规则。需要注意的是,不要同其他的主题或者模块重名。对于本地的主题,使用一个前缀来保证命名的唯一性是个好办法,例如example.com的主题可以叫做ex_themename。

.info文件会被缓存,因此对这个文件的修改需要清空缓存才能获得正确的结果。

.info文件也可以指定Drupal管理界面如何设置主题。

##编码

文件必须用UTF-8无BOM的方式进行保存。

##内容

Drupal能够识别下面的键,如果info文件重没有包含某些可选键,Drupal会使用这些键的缺省值。在核心主题中有相应的例子可供参考。

###name

现在在内部机读名称之外,可以独立设置可供人阅读的名称。相对于机读名称,这一名称对于可选字符的限制较少。

name = A fantasy name

###description

对主题的简短描述。这段描述会在位于“Administer > Site building > themes”的主题选择页面显示。

description = Tableless multi-column theme designed for blogs.

###screenshot

这个可选键告诉Drupal在哪里找到这个主题的缩略图,同样用于主题选择页面。如果这个键被省略,Drupal会在主题目录中查找”screenshot.png”文件。

screenshot = screenshot.png

参考:为管理页面创建截图

###version

version在主题被打包发布时会由drupal.org自动添加。所以如果要创建要发布到官网的主题,可以省略这一内容。然而,如果你的主题不准备在官网发布,就可以随便使用什么字符串来给这个键赋值了。

version = 1.0

###core

从Drupal 6以后,所有的主题和模块的.info文件必须指明兼容的Drupal核心的主版本。这个值会同DRUPAL_CORE_COMPATIBILITY这一常量进行比较。如果不符合,这个主题会被禁用。

core = 6.x

drupal.org打包脚本会根据Drupal核心版本为每个发布节点自动设置这个值,所以用户从drupal.org下载主题时总会得到正确的版本。对于其他方式的下载和安装,这个项是很有帮助的。

###engine

主题所使用的引擎。

Drupal 6:如果没有指定这个值,会假设这个主题是独立的,也就是用“.theme”实现的。多数主题会使用phptemplate作为缺省引擎。

Drupal 7:Drupal 7中用PHPTemplate为缺省引擎,这一行不再必要。

engine = phptemplate

###base theme

子主题可以声明一个基主题。这使得主题可以继承,意味着基主题的的资源可以在子主题中进行复用。子主题也能够作为其他子主题的基主题,形成多级继承的模式。base theme的值是基主题的机读名称,下面代码来自于Minnelli,Garland的子主题。

base theme = garland

《子主题,结构和继承》一文描述了更多细节。

###regions

区域的定义来自于主题info中的’regions’,后面的中括号中代表这个区域的机读名称,值则是标题。例如regions[theRegion] = The region name.

如果没有定义regions,则会假设使用如下定义

Drupal 6的缺省区域

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

Drupal 7的缺省区域

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

用户可以根据自身需求进行定义。

如果在Drupal 7中进行区域定义,会强制用户定义regions[content] = content。如果需要使用任何缺省区域,也都需要重新定义,这方面内容可以参考《块,内容和区域》一文。

###features

很多主题输出的页面元素都可以在主题的配置页面上设置开关。”features”键控制了哪些复选框出现在主题的配置页面上。注释掉部分features,相对应的复选框就会隐藏,然而如果注释掉所有的,那么就会缺省显示所有的复选框。

下面的例子展示了所有features控制的元素。

Drupal 6 features

primary_links 和 secondary_links被注释,他们对应的复选框会被隐藏。

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

Drupal 7 features

features[] = logo
features[] = name
features[] = slogan
features[] = node_user_picture
features[] = comment_user_picture
features[] = comment_user_verification
features[] = favicon
features[] = main_menu
features[] = secondary_menu

更多细节可以参考《定制主题设置项》

###theme settings

可以利用info文件中的这一配置来设置features中的选中状态。用settings[toggle_"feature"] = 0 来设置某个feature为未选状态。

例如缺省设置:

Drupal 7

settings[toggle_logo] = 1
settings[toggle_name] = 1
settings[toggle_slogan] = 1
settings[toggle_node_user_picture] = 1
settings[toggle_comment_user_picture] = 1
settings[toggle_comment_user_verification] = 1
settings[toggle_favicon] = 1
settings[toggle_main_menu] = 1
settings[toggle_secondary_menu] = 1

这个设置的更多细节可以参考《高级主题设置》

###stylesheets

一般来说,主题缺省会使用style.css,并可以利用drupal_add_css的方式在template.php中增加附加的css。Drupal 6开始,主题还可以利用info文件来添加css。

stylesheets[all][] = theStyle.css

在Drupal 7中,主题不再缺省使用style.css。

《样式表》一节提供了这方面的更多信息。

###scripts

传统上,主题可以在template.php中使用drupal_add_js来引入js文件。从Drupal 6.x开始,如果主题目录下存在一个名为stript.js的文件,那么这个文件会被自动包含。然而,在Drupal 7中这一行为再次变更,一个脚本文件只有在scripts中声明,才会被导入。

scripts[] = myscript.js

《JavaScript和jQuery》一节可以找到更多这方面的内容。

###php

这里定义对PHP版本的最低需求。这一项的缺省值来自于常量DRUPAL_MINIMUM_PHP,代表核心需要的最低PHP版本。主题可以声明需要一个更高的版本,当然,一般不建议这样做。

php = 4.3.3

###核心主题的info文件实例

Garland

Garland info

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"

Garland的子主题:Minnelli

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.org的打包脚本生成的,不需要手工添加project和时间戳。第一节中的version是用来从其他途径获取你的主题的用户区别版本需求使用的。

Avatar
崔秀龙

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

comments powered by Disqus
下一页