解析theme()

drupal_render()只是对theme()的调用做了包装,真正做任务的还是theme()。

function theme($hook, $variables = array()) {
... ...
}

theme()的开头检查了module_load_all()是否有执行。theme()只能在所有模块装入后才能执行。


// If called before all modules are loaded, we do not necessarily have a full
// theme registry to work with, and therefore cannot process the theme
// request properly. See also _theme_load_registry().
if (!module_load_all(NULL) && !defined(‘MAINTENANCE_MODE‘)) {
throw new Exception(t(‘theme() may not be called until all modules are loaded.‘));
}

theme_get_registry()返回所有的theme hooks。

$hooks = theme_get_registry(FALSE);

参数$hook可以是一个数组,包含所有可用的备选theme hook。theme()会取第一个存在的theme hook。


// If an array of hook candidates were passed, use the first one that has an
// implementation.
if (is_array($hook)) {
foreach ($hook as $candidate) {
if (isset($hooks[$candidate])) {
break;
}
}
$hook = $candidate;
}

处理theme hook suggestion。什么是theme hook
suggestion?以请求q=node/sports/5为例,可以优先匹配node__sports__5,再匹配node__sports,最后再匹配node。如果存在node__sports__5的theme
hook,则后面的就不予考虑,后面的以此类推。注意theme hook suggestion是以双下划线分开的。


// If there‘s no implementation, check for more generic fallbacks. If there‘s
// still no implementation, log an error and return an empty string.
if (!isset($hooks[$hook])) {
// Iteratively strip everything after the last ‘__‘ delimiter, until an
// implementation is found.
while ($pos = strrpos($hook, ‘__‘)) {
$hook = substr($hook, 0, $pos);
if (isset($hooks[$hook])) {
break;
}
}
if (!isset($hooks[$hook])) {
// Only log a message when not trying theme suggestions ($hook being an
// array).
// 只有$hook是数组的时候才记录日志信息
if (!isset($candidate)) {
watchdog(‘theme‘, ‘Theme hook %hook not found.‘, array(‘%hook‘ => $hook), WATCHDOG_WARNING);
}
return ‘‘;
}
}

$info保存当前hook信息。$theme_path是一个全局变量,在theme()执行过程中会替换为当前theme
hook对应的路径,theme()后面会还原回来。


$info = $hooks[$hook];
global $theme_path;
$temp = $theme_path;
// point path_to_theme() to the currently used theme path:
$theme_path = $info[‘theme path‘];

引入$info[‘includes‘]相关文件。


// Include a file if the theme function or variable processor is held
// elsewhere.
if (!empty($info[‘includes‘])) {
foreach ($info[‘includes‘] as $include_file) {
include_once DRUPAL_ROOT . ‘/‘ . $include_file;
}
}

重新整理$variables数组。


// If a renderable array is passed as $variables, then set $variables to
// the arguments expected by the theme function.
if (isset($variables[‘#theme‘]) || isset($variables[‘#theme_wrappers‘])) {
$element = $variables;
$variables = array();
if (isset($info[‘variables‘])) {
foreach (array_keys($info[‘variables‘]) as $name) {
if (isset($element["#$name"])) {
$variables[$name] = $element["#$name"];
}
}
}
else {
$variables[$info[‘render element‘]] = $element;
}
}

// Merge in argument defaults.
if (!empty($info[‘variables‘])) {
$variables += $info[‘variables‘];
}
elseif (!empty($info[‘render element‘])) {
$variables += array($info[‘render element‘] => array());
}

定义theme hook时,允许定义theme hook执行时必需的变量和默认值。可以使用render
element定义单个变量,也可以用variables定义多个变量。用render element定义单个变量的例子:


function practice_theme() {
return array(
‘flash_messages‘ => array(
‘render element‘ => ‘messages‘,
‘template‘ => ‘flash_messages‘,
),
);
}

// 整理后的$variables大致是这样:
$variables = array(
‘messages‘ => $element,
);

用variables定义多个变量的例子:


function practice_theme() {
return array(
‘flash_messages‘ => array(
‘variables‘ => array(
‘foo‘ => array(),
‘bar‘ => array()
),
‘template‘ => ‘flash_messages‘,
),
);
}

// 整理后的$variables大致是这样:
$variables = array(
‘foo‘ => isset($element[‘foo‘]) ? $element[‘foo‘] : array(),
‘bar‘ => isset($element[‘bar‘]) ? $element[‘bar‘] : array(),
);

如果$info[‘base hook‘]不为空,则后面调用的preprocess functions和process
functions要是base hook的,不能是当前$hook的。但还是要保证theme_hook_suggestion是当前$hook。


// If the hook is a suggestion of a base hook, invoke the variable processors of
// the base hook, but retain the suggestion as a high priority suggestion to
// be used unless overridden by a variable processor function.
if (isset($info[‘base hook‘])) {
$base_hook = $info[‘base hook‘];
$base_hook_info = $hooks[$base_hook];
// Include files required by the base hook, since its variable processors
// might reside there.
if (!empty($base_hook_info[‘includes‘])) {
foreach ($base_hook_info[‘includes‘] as $include_file) {
include_once DRUPAL_ROOT . ‘/‘ . $include_file;
}
}
if (isset($base_hook_info[‘preprocess functions‘]) || isset($base_hook_info[‘process functions‘])) {
$variables[‘theme_hook_suggestion‘] = $hook;
$hook = $base_hook;
$info = $base_hook_info;
}
}

调用preprocess functions和process
functions。这两类函数有两个目的,一是处理$variables,二是处理theme hook
suggestion。theme_hook_suggestion的优先级大于theme_hook_suggestions(注意是suggestion复数),theme_hook_suggestions再安装FILO先进后出的原则匹配,最后加入的优先级最高。


// Invoke the variable processors, if any. The processors may specify
// alternate suggestions for which hook‘s template/function to use.
if (isset($info[‘preprocess functions‘]) || isset($info[‘process functions‘])) {
$variables[‘theme_hook_suggestions‘] = array();
foreach (array(‘preprocess functions‘, ‘process functions‘) as $phase) {
if (!empty($info[$phase])) {
foreach ($info[$phase] as $processor_function) {
if (function_exists($processor_function)) {
// We don‘t want a poorly behaved process function changing $hook.
$hook_clone = $hook; // 不要让某些人将$hook搞坏了
$processor_function($variables, $hook_clone);
}
}
}
}
// If the preprocess/process functions specified hook suggestions, and the
// suggestion exists in the theme registry, use it instead of the hook that
// theme() was called with. This allows the preprocess/process step to
// route to a more specific theme hook. For example, a function may call
// theme(‘node‘, ...), but a preprocess function can add ‘node__article‘ as
// a suggestion, enabling a theme to have an alternate template file for
// article nodes. Suggestions are checked in the following order:
// - The ‘theme_hook_suggestion‘ variable is checked first. It overrides
// all others.
// - The ‘theme_hook_suggestions‘ variable is checked in FILO order, so the
// last suggestion added to the array takes precedence over suggestions
// added earlier.
$suggestions = array();
if (!empty($variables[‘theme_hook_suggestions‘])) {
$suggestions = $variables[‘theme_hook_suggestions‘];
}
if (!empty($variables[‘theme_hook_suggestion‘])) {
$suggestions[] = $variables[‘theme_hook_suggestion‘];
}
foreach (array_reverse($suggestions) as $suggestion) {
if (isset($hooks[$suggestion])) {
$info = $hooks[$suggestion];
break;
}
}
}

产生输出可以用函数也可以用模版。$info[‘function‘]表示调用函数产生输出。


if (isset($info[‘function‘])) {
if (function_exists($info[‘function‘])) {
$output = $info[‘function‘]($variables);
}
}

$info[‘template‘]表示使用模板产生输出。theme_render_template()是默认的模板输出函数,.tpl.php是默认的模板文件扩展名。

// Default render function and extension.
$render_function = ‘theme_render_template‘;
$extension = ‘.tpl.php‘;

不同的theme engine允许有不同的模板输出函数和模板文件扩展名。


// The theme engine may use a different extension and a different renderer.
global $theme_engine;
if (isset($theme_engine)) {
if ($info[‘type‘] != ‘module‘) {
if (function_exists($theme_engine . ‘_render_template‘)) {
$render_function = $theme_engine . ‘_render_template‘;
}
$extension_function = $theme_engine . ‘_extension‘;
if (function_exists($extension_function)) {
$extension = $extension_function();
}
}
}

通过template_preprocess()为$varialbes添加默认变量。使用$variables[‘directory‘]判断template_preprocess()有没有执行过。


// In some cases, a template implementation may not have had
// template_preprocess() run (for example, if the default implementation is
// a function, but a template overrides that default implementation). In
// these cases, a template should still be able to expect to have access to
// the variables provided by template_preprocess(), so we add them here if
// they don‘t already exist. We don‘t want to run template_preprocess()
// twice (it would be inefficient and mess up zebra striping), so we use the
// ‘directory‘ variable to determine if it has already run, which while not
// completely intuitive, is reasonably safe, and allows us to save on the
// overhead of adding some new variable to track that.
if (!isset($variables[‘directory‘])) {
$default_template_variables = array();
template_preprocess($default_template_variables, $hook);
$variables += $default_template_variables;
}

调用模板输出函数$render_function产生输出内容。


// Render the output using the template file.
$template_file = $info[‘template‘] . $extension;
if (isset($info[‘path‘])) {
$template_file = $info[‘path‘] . ‘/‘ . $template_file;
}
$output = $render_function($template_file, $variables);

Drupal使用PHPTemplate作为默认的主题引擎。PHPTemplate的输出函数theme_render_template()很简单,include模板文件就OK。


/**
* Renders a system default template, which is essentially a PHP template.
*
* @param $template_file
* The filename of the template to render.
* @param $variables
* A keyed array of variables that will appear in the output.
*
* @return
* The output generated by the template.
*/
function theme_render_template($template_file, $variables) {
// Extract the variables to a local namespace
extract($variables, EXTR_SKIP);

// Start output buffering
ob_start();

// Include the template file
include DRUPAL_ROOT . ‘/‘ . $template_file;

// End buffering and return its contents
return ob_get_clean();
}

最后,theme()还原$theme_path,返回输出$output。

// restore path_to_theme()
$theme_path = $temp;
return $output;

解析theme(),码迷,mamicode.com

时间: 2024-07-29 11:51:51

解析theme()的相关文章

DispatcherServlet详解

DispatcherServlet 和其它web框架一样,Spring的web框架是一个请求驱动的web框架,其设计围绕一个中心的servlet进行,它能将请求分发给控制器,并提供其它功能帮助web应用开发.然而,Spring的DispatcherServlet所做的不仅仅是这些,它和Spring的IoC容器完全集成在一起,从而允许你使用Spring的其它功能. 下图展示了DispatcherServlet对请求的处理流程.熟悉设计模式的读者可能会发现DispatcherServlet应用了"F

Android中资源管理机制详细分析

尊重原创:http://blog.csdn.net/yuanzeyao/article/details/42386549 在Android中,所有的资源都在res目录下存放,包括drawable,layout,strings,anim等等,当我们向工程中加入任何一个资源时,会在R类中相应会为该 资源分配一个id,我们在应用中就是通过这个id来访问资源的,相信做过Andorid开发的朋友对于这些肯定不会陌生,所以这个也不是我今天想要说的,我今天想和大家一起学习的是Android是如何管理资源的,在

Theme皮肤文件(json解析、多文件管理)

一  官方教程 http://developer.egret.com/cn/github/egret-docs/extension/EUI/skin/theme/index.html 二 thm主题文件解析 default.thm.json皮肤主题文件范例: { "skins": { "eui.Button": "resource/eui_skins/ButtonSkin.exml" }, "autoGenerateExmlsList&

Android Service完全解析,关于服务你所需知道的一切(上)

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11952435 相信大多数朋友对Service这个名词都不会陌生,没错,一个老练的Android程序员如果连Service都没听说过的话,那确实也太逊了.Service作为Android四大组件之一,在每一个应用程序中都扮演着非常重要的角色.它主要用于在后台处理一些耗时的逻辑,或者去执行某些需要长期运行的任务.必要的时候我们甚至可以在程序退出的情况下,让Service在后台继续保持

Android ActionBar完全解析,使用官方推荐的最佳导航栏(下)

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工,英文好的朋友也可以直接去读原文. http://developer.android.com/guide/topics/ui/actionbar.html 限于篇幅的原因,在上篇文章中我们只学习了ActionBar基础部分的知识,那么本篇文章我们将接着上一章的内容继续学习,探究一下ActionBar

Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/18234477 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工,英文好的朋友也可以直接去读原文. http://developer.android.com/guide/topics/ui/actionbar.html Action Bar是一种新増的导航栏功能,在Android 3.0之后加入到系统的API当中,它标识了用户当前操作界面的位置,并提供了额外的用

Dialog与FragmentDialog源码解析

<代码里的世界> -UI篇 用文字札记描绘自己 android学习之路 转载请保留出处 by Qiao http://blog.csdn.net/qiaoidea/article/details/46402845 [导航] - 弹出式对话框各种方案 从仿QQ消息提示框来谈弹出式对话框的实现方式 (Dialog,PopupWind,自定义View,Activity,FragmentDialog) - Dialog源码解析 从源码上看Dialog与DialogFragment 1.概述 前一篇写了

Android应用setContentView与LayoutInflater加载解析机制源码分析

[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处,尊重分享成果] 1 背景 其实之所以要说这个话题有几个原因: 理解xml等控件是咋被显示的原理,通常大家写代码都是直接在onCreate里setContentView就完事,没怎么关注其实现原理. 前面分析<Android触摸屏事件派发机制详解与源码分析三(Activity篇)>时提到了一些关于布局嵌套的问题,当时没有深入解释. 所以接下来主要分析的就是View或者ViewGroup对象是如何添加至应用程

android源码解析(十七)--&gt;Activity布局加载流程

好吧,终于要开始讲讲Activity的布局加载流程了,大家都知道在Android体系中Activity扮演了一个界面展示的角色,这也是它与android中另外一个很重要的组件Service最大的不同,但是这个展示的界面的功能是Activity直接控制的么?界面的布局文件是如何加载到内存并被Activity管理的?android中的View是一个怎样的概念?加载到内存中的布局文件是如何绘制出来的? 要想回答这些问题,我们就需要对android的界面加载与绘制流程有所了解,这里我们先来学习一下Act