Drupal 6 Theme系统

Published under

一、原理
二、流程
1. 系统调用函数theme(),进入theme系统;
2. init_theme()
3. theme_get_registry(),获得theme_registry列表;
4. 遍历theme_registry,选取当前调用的theme单元;
5. 判断该theme单元是否有[‘file’]、[‘path’],有则引用该文件;
6. 如果当前theme存在function,则调用该function;
7. 否则当前theme为template调用。默认渲染函数为theme_render_template(),可以自定义渲染函数和模板文件扩展名。
8. 执行该theme单元的preprocess functions注册函数,根据传进来的变量$variables['template_file']中系统中寻找合适的模板文件,执行渲染函数theme_render_template()。
三、API
1. hook_theme(),应用于module、theme_engine、theme里,返回theme单元数组;
2. 返回数组的参数:
Array(
‘hook_name’ => array(
‘function’ => ‘’, //调用函数,若省略默认为theme_hook_name;
‘template’ => ‘’, //模板文件,与function只能有一个存在;
‘arguments’ => array(), //变量参数,将传入模板文件的变量列表里;
‘file’ => ‘’ //附加文件;
‘path’ => ‘’ //附加文件的路径,若省略默认为当前模块的路径;
'preprocess functions' => array() //附加的变量预处理函数列表;
'original hook' => ‘’ //原始hook单元;
)
)
四、theme_registry的生成
1. 初始化theme(init_theme())完成之后系统回调_theme_load_registry函数;如果load不到相应的theme_registry列表,则调用_theme_build_registry(),开始生成theme_registry列表。
2. 在_theme_build_registry()内部,系统先生成所有module的theme列表,然后在此基础上调用base_theme的theme_engine、theme,theme的theme_engine、theme,生成最后的theme列表;在这些执行步骤中,后面的theme列表生成是以前面的theme列表为基础,从而在每个theme单元上形成类似于堆栈的处理链;
3. 在_theme_process_registry()内部,系统调用该类型(module, theme_engine, theme)的hook_theme()函数,对返回的theme单元数组填充必要的数据(是function调用还是template调用、preprocess functions函数链等),然后添加到theme列表中;