Render Layer — render.ts
The Handlebars template rendering wrapper.
Design principle: thin templates
Section titled “Design principle: thin templates”The templates only carry the file skeleton (header comment, imports, namespace open/close). Per-token literal formatting is entirely done in the target’s TypeScript, which feeds the assembled body string to the template.
target.ts: format token by token → assembled into a body string ▼render.ts: render('android/LhTokens.kt.hbs', { package, imports, body }) ▼template: header comment + package declaration + imports + {{{body}}}The benefit: no complicated handlebars logic inside the templates — changing a literal format only touches the target’s TS, and changing the file skeleton only touches the template, without interfering with each other.
Functions
Section titled “Functions”loadTemplate(name: string): HandlebarsTemplateDelegate
Section titled “loadTemplate(name: string): HandlebarsTemplateDelegate”An internal function. Reads codegen/templates/<name> by template name:
const compiled = Handlebars.compile(source, { noEscape: true })cache.set(name, compiled) // The compiled result is cached to avoid recompilationThe template directory is given by the TEMPLATES_DIR constant (codegen/templates).
render<T>(templateName: string, data: T): string
Section titled “render<T>(templateName: string, data: T): string”The only public interface:
return loadTemplate(templateName)(data)Every target needs only this one call.
No custom helpers
Section titled “No custom helpers”The templates register no handlebars helpers at all; only built-in capabilities are used:
| Syntax | Purpose |
|---|---|
{{{ }}} |
Triple braces plus noEscape, for inserting the pre-formatted body |
{{#each}} |
Iterating arrays such as the import list |
The 7 templates
Section titled “The 7 templates”| Template | Artifacts |
|---|---|
android/LhTokens.kt.hbs |
LhTokens.kt and LhDarkTokens.kt (shared by light and dark) |
ios/LhTokens.swift.hbs |
LhTokens.swift and LhDarkTokens.swift |
ios/LhShadow.swift.hbs |
LhShadow.swift (a static template, with no body slot) |
harmony/LhTokens.ets.hbs |
LhTokens.ets and LhDarkTokens.ets |
harmony/LhShadow.ets.hbs |
LhShadow.ets (a static template) |
c/lh_tokens.h.hbs |
lh_tokens.h |
c/lh_dark_tokens.h.hbs |
lh_dark_tokens.h |
See Template List.
Copyright © 2024-2026 FengMo WenXue