glimr/loom/generator
Code Generator
Transforms parsed template ASTs into executable Gleam code. Generates functions that build HTML strings using the runtime helpers for escaping, conditionals, and loops.
Types
Maps component names to their expected data fields. Used to generate default values for missing props when rendering components.
pub type ComponentDataMap =
dict.Dict(String, List(#(String, String)))
Information about a component’s slot usage. Tracks whether the component uses a default slot and what named slots it accepts.
pub type ComponentSlotInfo {
ComponentSlotInfo(
has_default_slot: Bool,
named_slots: List(String),
)
}
Constructors
-
ComponentSlotInfo( has_default_slot: Bool, named_slots: List(String), )
Maps component names to their slot information. Used when generating component calls to determine which slot arguments to include.
pub type ComponentSlotMap =
dict.Dict(String, ComponentSlotInfo)
The result of code generation. Contains the module name and the complete generated Gleam source code ready to be written to a file.
pub type GeneratedCode {
GeneratedCode(module_name: String, code: String)
}
Constructors
-
GeneratedCode(module_name: String, code: String)
Values
pub fn extract_named_slots(
template: parser.Template,
) -> List(String)
Extracts all named slot references from a template. Walks the AST to find @slot(“name”) directives and returns a deduplicated list of slot names.
pub fn extract_slot_info(
template: parser.Template,
) -> ComponentSlotInfo
Extracts complete slot information from a component template. Returns whether it has a default slot and the list of named slots it accepts.
pub fn generate(
template: parser.Template,
module_name: String,
is_component: Bool,
view_file: option.Option(gleam_parser.ParsedViewFile),
component_data: dict.Dict(String, List(#(String, String))),
component_slots: dict.Dict(String, ComponentSlotInfo),
) -> GeneratedCode
Generates Gleam code from a parsed template. Takes the AST, module metadata, and type information to produce complete source code with imports and the html function.