Templates & Projector Logic
Templates & Projector
Section titled “Templates & Projector”The Projector (src/projector.rs) is the engine responsible for bridging the gap between Semantic Vyasa commands and the output (usually HTML).
Core Concept
Section titled “Core Concept”Instead of hardcoding HTML generation in Rust (e.g. write!(w, "<h1>{}</h1>", title)), the compiler uses Templates defined in Vyasa itself (stdlib.vy or context.vy).
The Expansion Loop
Section titled “The Expansion Loop”- Input: Semantic Node (e.g.
`heading { The Title }). - Lookup: The Projector checks
env.templatesfor a matchingheadingtemplate. - Expansion: If found, it replaces the Semantic Node with the template’s body, interpolating variables.
- Recursion: The output is processed again (allowing templates to use other templates).
Template Definition
Section titled “Template Definition”Templates are defined using the template command (usually sugar syntax in .vy):
`template { target="heading" `h2 { $.text }}Interpolation Syntax
Section titled “Interpolation Syntax”$.text: Replaced by the semantic command’s children.$.var: Replaced by the value of attributevarfrom the semantic command.$.context.key: (Advanced) Access global context.
Control Flow
Section titled “Control Flow”The Projector supports basic logic:
if Command
Section titled “if Command”Conditionals based on attribute values.
`if { lhs="$.type" op="eq" rhs="warning" `div { class="alert warning" ... }}each Command
Section titled “each Command”Iterating over lists (collections).
`each { "work.chapters" `li { `a { href="$.url" $.title } }}View vs Collection Templates
Section titled “View vs Collection Templates”- View Template: Used to render a single source file. Defines the
bodycommand behavior. - Collection Template: Used to generate an index page (e.g. Table of Contents) by iterating over build stats (
work.items).