How vapor/template-kit works
Project type and purpose
• Project type: Open-source Swift library (Vapor ecosystem).
• Purpose: Parse template files, build an AST, and render text output by merging templates with user‐provided data.
Main architectural components
A. Template Source Input
B. Parser & AST
C. Data Model & Encoder
D. Rendering Pipeline
E. Tag Extensions
F. Caching & Utilities
G. Integration Point (ViewRenderer / Vapor)
Component breakdown and interactions
1. Template Source Input
• Template files (e.g. “index.leaf”), raw text.
2. Parser & AST (Sources/TemplateKit/AST & Pipeline/TemplateParser)
• TemplateByteScanner: tokenizes input.
• TemplateParser: produces AST nodes (TemplateSyntax, TemplateTag, TemplateExpression, etc.).
• AST data structures (TemplateConditional, TemplateIterator, TemplateRaw, etc.)
3. Data Model & Encoder (Sources/TemplateKit/Data)
• TemplateData: unified data type.
• TemplateDataEncoder: encodes Swift types (structs, primitives) into TemplateData.
4. Rendering Pipeline (Sources/TemplateKit/Pipeline & ViewRenderer)
• TemplateRenderer: walks the AST, invokes TagRenderer for each tag node.
• PlaintextRenderer: handles nodes that are raw text or unparsed segments.
• TemplateSerializer: serializes AST for caching or reuse.
5. Tag Extensions (Sources/TemplateKit/Tag)
• TagRenderer & TagContext: generic plugin interfaces.
• Built-in tags: Var, If (conditional), Loop (iterator), DateFormat, Uppercase/Lowercase, Contains, Count, Print, Raw, Comment, Capitalize.
6. Caching & Utilities (Sources/TemplateKit/Pipeline/ASTCache & Utilities)
• ASTCache: caches parsed AST keyed by template path.
• Utilities: HTMLEscape, TemplateError, Exports.
7. Integration Point
• ViewRenderer & View: conform to Vapor’s View protocol.
Architectural patterns & design principles
• Separation of Concerns: parsing, data encoding, rendering, tagging, and caching are distinct modules.
• Plug-in architecture: tag implementations conform to a shared TagRenderer interface.
• Immutable AST: parsing and rendering phases are decoupled to allow caching and reuse.
• Swift Package Manager: Package.swift defines dependencies (no external services).