以webclient.xml为例,这是WebClient组件对应的模板文件(WebClient是App启动时实例化的入口组件)
/* odoo/addons/web/static/src/webclient/webclient.xml */
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="web.WebClient" owl="1">
<t t-if="!state.fullscreen">
<NavBar/>
</t>
<ActionContainer/>
<MainComponentsContainer/>
</t>
</templates>
编译后的代码
(function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
// Template name: "web.WebClient"
const comp1 = app.createComponent(`NavBar`, true, false, false, true);
const comp2 = app.createComponent(`ActionContainer`, true, false, false, true);
const comp3 = app.createComponent(`MainComponentsContainer`, true, false, false, true);
return function template(ctx, node, key = "") {
let b2,b3,b4;
if (!ctx['state'].fullscreen) {
b2 = comp1({}, key + `__1`, node, this, null);
}
b3 = comp2({}, key + `__2`, node, this, null);
b4 = comp3({}, key + `__3`, node, this, null);
return multi([b2, b3, b4]);
}
})
编译后的代码是通过new Function方式动态创建的匿名函数,创建组件时执行的是匿名函数返回的闭包template