渲染層 render.ts
هذا المحتوى غير متوفر بلغتك بعد.
Handlebars 模板渲染封裝。
設計原則:薄模板
Section titled “設計原則:薄模板”模板只負責檔案骨架(頭部註解、import、命名空間開合)。逐令牌的字面值格式化全部在 target 的 TS 裡完成,以組裝好的 body 字串餵給模板。
target.ts: 逐令牌格式化 → 拼成 body 字串 ▼render.ts: render('android/LhTokens.kt.hbs', { package, imports, body }) ▼模板: 頭部註解 + package 宣告 + imports + {{{body}}}好處:模板裡沒有複雜的 handlebars 邏輯,改字面值格式只動 target 的 TS,改檔案骨架只動模板,互不干擾。
loadTemplate(name: string): HandlebarsTemplateDelegate
Section titled “loadTemplate(name: string): HandlebarsTemplateDelegate”內部函式。按模板名讀 codegen/templates/<name>:
const compiled = Handlebars.compile(source, { noEscape: true })cache.set(name, compiled) // 編譯結果快取,避免重複編譯模板目錄由常數 TEMPLATES_DIR 指定(codegen/templates)。
render<T>(templateName: string, data: T): string
Section titled “render<T>(templateName: string, data: T): string”唯一對外介面:
return loadTemplate(templateName)(data)各 target 只需要這一行呼叫。
沒有自訂 helper
Section titled “沒有自訂 helper”模板沒有註冊任何 handlebars helper,只用內建能力:
| 語法 | 用途 |
|---|---|
{{{ }}} |
三重花括號 + noEscape,插入預格式化的 body |
{{#each}} |
遍歷 import 清單等陣列 |
| 模板 | 產物 |
|---|---|
android/LhTokens.kt.hbs |
LhTokens.kt 和 LhDarkTokens.kt(明暗共用) |
ios/LhTokens.swift.hbs |
LhTokens.swift 和 LhDarkTokens.swift |
ios/LhShadow.swift.hbs |
LhShadow.swift(靜態模板,無 body 插槽) |
harmony/LhTokens.ets.hbs |
LhTokens.ets 和 LhDarkTokens.ets |
harmony/LhShadow.ets.hbs |
LhShadow.ets(靜態模板) |
c/lh_tokens.h.hbs |
lh_tokens.h |
c/lh_dark_tokens.h.hbs |
lh_dark_tokens.h |
詳見 模板清單。
Copyright © 2024-2026 FengMo WenXue