drupal_render()函数接收一个结构化的数组作为参数,然后调用theme()输出HTML。

function drupal_render(&$elements) {
... ...
}

$elements['#printed']控制是否已经执行过,避免重复执行。在drupal_render()的开头检查它是否有设置,对应的,在drupal_render()的结尾设置它。

// Do not print elements twice.
if (!empty($elements['#printed'])) {
return '';
} // 在drupal_render()结尾会设置#printed
$elements['#printed'] = TRUE;

$elements['#cache']控制是否允许使用缓存。如果有缓存,就直接返回,不用再重复执行后面的代码。|

// Try to fetch the element's markup from cache and return.
if (isset($elements['#cache'])) {
$cached_output = drupal_render_cache_get($elements);
if ($cached_output !== FALSE) {
return $cached_output;
}
} // 在drupal_render()结尾保存缓存
if (isset($elements['#cache'])) {
drupal_render_cache_set($output, $elements);
}

$elements['#pre_render']是一个function数组,drupal_render()会依次执行其中的function,这样可以在render之前对$elements再做一些修改。

// Make any final changes to the element before it is rendered. This means
// that the $element or the children can be altered or corrected before the
// element is rendered into the final text.
if (isset($elements['#pre_render'])) {
foreach ($elements['#pre_render'] as $function) {
if (function_exists($function)) {
$elements = $function($elements);
}
}
}

$elements包含两类项目:properties和children。properties以#开头,其它都是children。

// Get the children of the element, sorted by weight.
$children = element_children($elements, TRUE);

$elements['#children']是一个字符串,临时保存drupal_render()输出的内容。

// Initialize this element's #children, unless a #pre_render callback already
// preset #children.
if (!isset($elements['#children'])) {
$elements['#children'] = '';
}

$elements['#theme']控制调用的theme function,结果保存在临时变量$elements['#children']。

// Call the element's #theme function if it is set. Then any children of the
// element have to be rendered there.
if (isset($elements['#theme'])) {
$elements['#children'] = theme($elements['#theme'], $elements);
}

如果$elements['#theme']没有被处理,对应的$elements['#children']就为空。这个时候,drupal_render()就循环$children,递归调用drupal_render()。注意这里返回的结果是串联在$elements['#children']。

// If #theme was not set and the element has children, render them now.
// This is the same process as drupal_render_children() but is inlined
// for speed.
if ($elements['#children'] == '') {
foreach ($children as $key) {
$elements['#children'] .= drupal_render($elements[$key]);
}
}

$elements['#theme_wrappers']是一个function数组,循环依次传入到theme()。注意这里返回的结果每次都会覆盖掉$elements['#children']。

// Let the theme functions in #theme_wrappers add markup around the rendered
// children.
if (isset($elements['#theme_wrappers'])) {
foreach ($elements['#theme_wrappers'] as $theme_wrapper) {
$elements['#children'] = theme($theme_wrapper, $elements);
}
}

$elements['#post_render']也是一个function数组,可以对生成的$elements['#children']做二次处理。注意这里返回的结果每次都会覆盖掉$elements['#children']。

// Filter the outputted content and make any last changes before the
// content is sent to the browser. The changes are made on $content
// which allows the output'ed text to be filtered.
if (isset($elements['#post_render'])) {
foreach ($elements['#post_render'] as $function) {
if (function_exists($function)) {
$elements['#children'] = $function($elements['#children'], $elements);
}
}
}

$elements['#prefix']和$elements['#suffix']允许对输出HTML做最后修饰。

$prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
$suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
$output = $prefix . $elements['#children'] . $suffix;

解析drupal_render()的更多相关文章

  1. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

  2. .NET Core中的认证管理解析

    .NET Core中的认证管理解析 0x00 问题来源 在新建.NET Core的Web项目时选择“使用个人用户账户”就可以创建一个带有用户和权限管理的项目,已经准备好了用户注册.登录等很多页面,也可 ...

  3. Html Agility Pack 解析Html

    Hello 好久不见 哈哈,今天给大家分享一个解析Html的类库 Html Agility Pack.这个适用于想获取某网页里面的部分内容.今天就拿我的Csdn的博客列表来举例. 打开页面  用Fir ...

  4. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  5. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  6. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

  7. Asp.Net WebApi核心对象解析(下篇)

    在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...

  8. 【知识必备】内存泄漏全解析,从此拒绝ANR,让OOM远离你的身边,跟内存泄漏say byebye

    一.写在前面 对于C++来说,内存泄漏就是new出来的对象没有delete,俗称野指针:而对于java来说,就是new出来的Object放在Heap上无法被GC回收:而这里就把我之前的一篇内存泄漏的总 ...

  9. SQL Server 数据加密功能解析

    SQL Server 数据加密功能解析 转载自: 腾云阁 https://www.qcloud.com/community/article/194 数据加密是数据库被破解.物理介质被盗.备份被窃取的最 ...

随机推荐

  1. bzoj1954 The xor-longest path

    Description  给定一棵n个点的带权树,求树上最长的异或和路径 Input The input contains several test cases. The first line of ...

  2. 最新iOS砸壳方式Frida (Mac OSX)

    1. 安装Frida 首先需要安装Python3,我下载的是 macOS 64-bit installer 安装,因Macbook本机自带python为2.7.x,故需要配置~/.bash_profi ...

  3. 关于收到谷歌邮件 Googlebot can't access your site 的解决方法

    最近一段时间一直都收到谷歌的邮件,而且“新锐工作室”的关键字在谷歌收录及排名都没有了.下面图为谷歌蜘蛛无法抓取网站的截图,如果你在谷歌网管工具里收到类似消息,说明也中招了.[Webmaster Too ...

  4. KVM虚拟机安装使用教程(Ubantu)

    背景: 公司在某电信机房有50台ubantu的实体机,机器配置的ip是192.168.100.x的ip,内存和cpu都是高端配置.假如哪些端口需要对外映射,就通过机房的防火墙完成端口映射. 100.1 ...

  5. java 调用shell命令

    原文:http://kongcodecenter.iteye.com/blog/1231177 Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar)   ...

  6. Android调用系统相机、自定义相机、处理大图片

    Android调用系统相机和自定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显示出来,该例子也会涉及到Android加载大图片时候的处理 ...

  7. java中引用的原理

    转自:http://blog.163.com/xubin_3@126/blog/static/112987702200962211145825/ 在Java中的引用类型,是指除了基本的变量类型之外的所 ...

  8. 关于如何在你的Web项目中实现对空间数据访问权限的控制(一)

    Wednesday, 23 JUNE 近来一直在研究关于如何在我的WebGIS项目中实现对空间数据(已发布在GeoServer上)进行权限管理的问题.虽然到目前为止没能找到一个完美的解决方案,但通过这 ...

  9. Andorid之Annotation框架初使用(二)

    Fragment: @EActivity(R.layout.fragments) public class MyFragmentActivity extends FragmentActivity { ...

  10. Hive QL——深入浅出学Hive

    第一部分:DDL DDL •建表 •删除表 •修改表结构 •创建/删除视图 •创建数据库 •显示命令 建表 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_ ...