You are here

template.php:覆写其它drupal主题函数

g089h515r806 的头像
Submitted by g089h515r806 on 星期六, 2008-09-13 02:13

如果你要覆写的主题函数不包含在基本列表中(block, box, comment, node, page),你需要将它告诉给PHPTemplate.

 

为了实现这一点,你需要在你主题目录下,创建一个template.php文件.这个文件以PHP开始标签<?php,但是这里不需要结束标签,推荐你不要使用结束标签.还有,在文件中,还需要包含用于主题覆写的存根(stubs),这些存根告诉引擎使用哪个模板文件,以及向其中传递哪些参数.

 

首先,你需要知道你要覆写哪个主题函数.你可以在Drupal API文档中找出一列这样的函数.我们在这里以theme_item_list()为例.

 

theme_item_list()大概是这样定义的:
<?php function theme_item_list($items = array(), $title = NULL) {  ?>

 

现在,你需要在你主题的template.php文件中加一个存根,如下所示:

<?php
/**
* Catch the theme_item_list function, and redirect through the template api
*/
function phptemplate_item_list($items = array(), $title = NULL) {
  // Pass to phptemplate, including translating the parameters to an associative array.
  // The element names are the names that the variables
  // will be assigned within your template.
  return _phptemplate_callback('item_list', array('items' => $items, 'title' => $title));
}
?>

我们将函数名中的“theme”替换为了“phptemplate”,并调用函数_phptemplate_callback()以将参数($items和$title)传递给PHPTemplate。

 

现在,你可以在你主题目录下,创建一个item_list.tpl.php模板文件了,它将用来替代原有的theme_item_list()。这个函数,与原有的theme_item_list()相比,应该采用相同的逻辑。

 

注意,你需要访问[ Administer -> Site building -> Themes ],从而刷新PHPTemplate的缓存,以识别出新添加的文件。但是从drupal4.6开始,就不需要这一步了。

 

除了Drupal标准的主题函数以外(API中所包含的),你还可以使用主题函数来覆写Drupal表单的外观。例如,对于由表单构建器函数构建的一个核心表单,你可以使用form_id来对其进行覆写。详情可参看这个实例