解析drupal_render()
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()的更多相关文章
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- .NET Core中的认证管理解析
.NET Core中的认证管理解析 0x00 问题来源 在新建.NET Core的Web项目时选择“使用个人用户账户”就可以创建一个带有用户和权限管理的项目,已经准备好了用户注册.登录等很多页面,也可 ...
- Html Agility Pack 解析Html
Hello 好久不见 哈哈,今天给大家分享一个解析Html的类库 Html Agility Pack.这个适用于想获取某网页里面的部分内容.今天就拿我的Csdn的博客列表来举例. 打开页面 用Fir ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- Asp.Net WebApi核心对象解析(下篇)
在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...
- 【知识必备】内存泄漏全解析,从此拒绝ANR,让OOM远离你的身边,跟内存泄漏say byebye
一.写在前面 对于C++来说,内存泄漏就是new出来的对象没有delete,俗称野指针:而对于java来说,就是new出来的Object放在Heap上无法被GC回收:而这里就把我之前的一篇内存泄漏的总 ...
- SQL Server 数据加密功能解析
SQL Server 数据加密功能解析 转载自: 腾云阁 https://www.qcloud.com/community/article/194 数据加密是数据库被破解.物理介质被盗.备份被窃取的最 ...
随机推荐
- linq to datatable 和lambda查询datatable
用Linq查询DataTable static DataTable table = new DataTable(); static DataColumn dc = new DataColumn(); ...
- Android 按钮长按下去重复执行某个动作,放开后停止执行动作
Android开发中,常遇到一种需求,即按钮长按下去重复执行某个动作,放开后停止执行动作.网上找了许多代码,都没有适合的,于是自己动手写了一个. 基本思路是:首先设置一个标识变量,用于标识是否处于按下 ...
- ExtJs2.0学习系列(12)--Ext.TreePanel之第一式
今天开始,我们就开始一起学习TreePanel了,道个歉,上篇的代码很乱阿. 我总是喜欢用最简单的例子开始,去理解最基本的使用方法,减少对i后面高级使用的干扰! TreePanel是继承自Panel, ...
- JS 判断PC、android、ios、微信浏览器
1.通过js userAgent来判断 <h1>判断访问此链接的操作系统</h1> <script> var Agents = new Array("An ...
- CentOS下的RPM包制作教程收集(rpmbuild)
http://www.jinbuguo.com/redhat/rpmbuild.html(中文手册) https://www.linuxidc.com/Linux/2016-09/134955.htm ...
- CentOS 6.9开启iptables的日志实现调试
系统日志配置在CentOS 5上叫syslog,而在CentOS 6上叫rsyslog(增强版的syslog),CentOS 5上的配置文件在/etc/syslog.conf下,而CentOS 6在/ ...
- HDU 4681 String(2013多校8 1006题 DP)
String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- MOSFET pair makes simple SPDT switch
With an n- and p-channel MOSFET, you can easily implement a single-pole double-throw (SPDT) switch t ...
- 【ASP】Window2008站点安全设置,IIS7/IIS7.5中目录执行权限的设置方法
最近帮一个朋友管理Window 2008服务器,发现有个站点是用asp写的,更可怕的是还有传说中的“上传漏洞”,在上传文件夹中有好多的可执行的asp.php文件,算是shell后门脚本吧.怎么处理这个 ...
- OpenERP实施记录(12):付款处理
本文是<OpenERP实施记录>系列文章的一部分. 1. 在前面的文章中,采购订单确认时自动生成了供应商发票,可以在 会计 > 供应商列表 > 供应商发票 查询,状态为&quo ...