Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
xtemplate internal
Search
yiminghe
August 07, 2014
Technology
2.1k
1
Share
xtemplate internal
implementation about xtemplate
yiminghe
August 07, 2014
More Decks by yiminghe
See All by yiminghe
小程序终端技术架构
yiminghe
0
280
支付宝小程序的开放架构
yiminghe
0
210
gitc2016 react based architecture
yiminghe
1
180
antd at qcon2016
yiminghe
1
240
react-based architecture
yiminghe
2
180
React Ecosystem At Ant Financial
yiminghe
4
2.3k
ant design preview
yiminghe
1
330
react best practice
yiminghe
3
230
react at alipay
yiminghe
43
4.5k
Other Decks in Technology
See All in Technology
毎日の作業を Claude Code 経由にしたら、 ノウハウがコードになった
kossykinto
1
1.4k
Purview 勉強会報告 Microsoft Purview 入門しようとしてみた
masakichixo
1
410
Purview Endpoint DLP 動かしてみた
kozakigh
0
410
2026年春のAgentCoreアプデ 細かいやつ全部まとめ
minorun365
4
230
Claude Code / Codex / Kiro に AWS 権限を 渡すとき、何を設計すべきか
k_adachi_01
5
1.4k
ESP32 IoTを動かしながらメモリ使用量を観測してみた話
zozotech
PRO
0
130
JTCでRedmine利用者2700人を実現した手法 第二部
nobuonakamura
0
110
写真で見るAWS Summit Singapore 2026
k_adachi_01
0
110
20260515 ログイン機能だけではないアカウント管理を全体で考える~サービス設計者向け~
oidfj
0
600
小さいVue.jsを30分で作る
hal_spidernight
0
160
ワールドカフェ再び、そしてゴール・ルール・ロール・ツール / World Café Revisited, and the Goals-Rules-Roles-Tools
ks91
PRO
0
170
Redmine次期バージョン7.0の注目新機能解説 — UI/UX強化と連携強化を中心に
vividtone
1
120
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.4k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.6k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
460
Reality Check: Gamification 10 Years Later
codingconduct
0
2.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
44k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.9k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Fireside Chat
paigeccino
42
3.9k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Transcript
xtemplate internal
[email protected]
承玉
xtemplate • Yet Another Extensible Template Engine • Docs: –
http://kissyteam.github.io/xtpl • Development Source: – https://github.com/kissyteam/xtemplate
功能简介 • 变量渲染/转义/设置 • 表达式 • 控制语句 • 层次访问 •
子模板 • 继承 • 宏 • 命令扩展 • 报错
变量渲染 • {{x}} – S.escapeHtml • {{{x}}} • {{set x=1}}
表达式 • + - * / % • && ||
=== >= <= • {{x+2}} • {{#if (x>2)}}xxx{{/if}}
控制语句 • {{#if (x)}}{{/if}} • {{#each (x)}}{{xindex}}{{this}}{{/each}} • {{#with (obj)}}{{y}}{{/with}}
– {{obj.y}}
层次访问 • {{#each (y)}}{{../../x}}{{x}}{{/each}} • { x: 1, y: [{
x: 2 }] }
子模板 • {{include (‘./x-tpl’)}} • x-tpl.html – {{x}} {{#each}}
继承 • Layout.xtpl – {{#block(‘default’)}}default{{/block}} – {{block(‘header’)}} • Page.xtpl –
{{extend(‘./layout.xtpl’)}} – {{#block(‘header’)}}header{{/block}}
宏 // 声明 {{#macro("test","param" default=1)}}param is {{param}} {{default}}{{/macro}} // 调用宏
{{macro("test","2")}} // => param is 2 1 {{macro("test", "2", 2)}} // => param is 2 2
命令扩展 • 同步 • 异步 • 块状 • 行内
报错处理 • {{x}}\n{{y ------------^ unclosed error at line 2
实现 • 设计语法 • 生成解析器 • 翻译代码 • 执行引擎 –
划分模块
设计语法 • {{mustache}} • 词法 • 语法
词法 • 识别基本元素 • Open • Id • operator
语法 • 描述程序结构 • Program • Statement xx • Tpl
{{x}}
解析器生成 • kison – https://github.com/yiminghe/kison – LALR 解析器生成工具 • 词法+语法
-> kison -> parse.js
parser.js • module • parse() – 标记语法树 – parse(‘my\n{{x}}’) program
Content: line:1 value: my\n tpl: line:2 Id: line:2 value: x
翻译代码 • 遍历语法树序列化生成 js 函数 var source = “var buffer
= new LinkedBuffer().head;”; If(node.type == ‘id’){ source+=‘buffer.write(context.’+ node.name+’)’; } else { … } source+=‘return LinkedBuffer’;
执行 • {{my}} • new Function(“context”, source)({my:1});
模板渲染拼接
include/extend 加载 • nodejs – fs.readFile • Browser – a
includes b // a.xtpl modulex.add(function(require){ return function(){ require('./b'); }; }) // b.xtpl .add(function(){ return function(){}; });
模块划分 • xtemplate/compiler 翻译代码 – 离线 (工具编译) – 在线(客户端浏览器) •
xtemplate runtime – 运行环境 • xtemplate requires [‘compiler’, ‘runtime’]
standalone • 完全无依赖,即放即用 • https://github.com/kissyteam/xtemplate/blob /master/build/xtemplate-standalone-debug.js • <script src=‘’></script>
• Questions?