glimr/loom/runtime

Template Runtime

Provides runtime functions for compiled Loom templates. Handles string concatenation, conditional rendering, loops, HTML escaping, and attribute management.

Types

Represents an HTML attribute. Can be a standard name-value pair or a boolean attribute that renders only when the condition is true.

pub type Attribute {
  Attribute(name: String, value: String)
  BoolAttribute(name: String, condition: Bool)
}

Constructors

  • Attribute(name: String, value: String)
  • BoolAttribute(name: String, condition: Bool)

Provides loop metadata for @each iterations. Contains index, count, and boolean flags for first/last/even/odd to enable conditional styling in templates.

pub type Loop {
  Loop(
    index: Int,
    iteration: Int,
    first: Bool,
    last: Bool,
    even: Bool,
    odd: Bool,
    count: Int,
    remaining: Int,
  )
}

Constructors

  • Loop(
      index: Int,
      iteration: Int,
      first: Bool,
      last: Bool,
      even: Bool,
      odd: Bool,
      count: Int,
      remaining: Int,
    )

Values

pub fn append(acc: String, value: String) -> String

Appends a value to the accumulator string. Used by compiled templates to build up the output HTML through successive string concatenation.

pub fn append_each(
  acc: String,
  items: List(item),
  render_fn: fn(String, item) -> String,
) -> String

Appends content for each item in a list. Folds over the items, calling the render function for each one to build up repeated template sections.

pub fn append_each_with_loop(
  acc: String,
  items: List(item),
  render_fn: fn(String, item, Loop) -> String,
) -> String

Appends content for each item with loop metadata. Provides a Loop record containing index, count, and position flags for conditional rendering based on iteration state.

pub fn append_if(
  acc: String,
  condition: Bool,
  render_fn: fn(String) -> String,
) -> String

Conditionally appends content based on a boolean. When true, calls the render function to append content. When false, returns the accumulator unchanged.

pub fn build_classes(items: List(#(String, Bool))) -> String

Builds a class string from conditional class entries. Takes a list of class name and boolean pairs, including only the classes where the condition is true.

pub fn build_styles(items: List(#(String, Bool))) -> String

Builds a style string from conditional style entries. Takes a list of style value and boolean pairs, including only the styles where the condition is true.

pub fn class(value: String) -> #(String, Bool)

Wraps a static class string as an always-true conditional. Use in :class lists to include static classes alongside conditional ones: :class=“[class(‘btn’), #(‘active’, is_active)]”

pub fn escape(value: String) -> String

Escapes HTML special characters to prevent XSS attacks. Converts &, <, >, “, and ’ to their HTML entity equivalents for safe rendering.

pub fn merge_attributes(
  base: List(Attribute),
  extra: List(Attribute),
) -> List(Attribute)

Merges extra attributes into a base list. Class and style attributes are concatenated, while other attributes override existing values with the same name.

pub fn render_attributes(attrs: List(Attribute)) -> String

Renders a list of attributes to an HTML attribute string. Escapes values and handles boolean attributes that only render their name when the condition is true.

pub fn style(value: String) -> #(String, Bool)

Wraps a static style string as an always-true conditional. Use in :style lists to include static styles alongside conditional ones: :style=“[style(‘color: red’), #(‘display: none’, hide)]”

Search Document