pomade

pomade is a Mustache library written in Gleam.

Types

Error is used to report an error from the API.

pub type Error {
  CompilationError(String)
  RuntimeError(String)
}

Constructors

  • CompilationError(String)

    CompilationError represents an error encountered either during scanning or during parsing.

  • RuntimeError(String)

    RuntimeError represents an error encountered during interpretation.

Template

opaque

Template represents a compiled template.

pub opaque type Template

Value is used to represent the input types that a Mustache template accepts. It also allows the API to accept heterogeneous dictionaries, which Gleam, understandably, does not support natively.

pub type Value =
  @internal Value

Values

pub fn bool(value: Bool) -> Value

bool creates a Value from a Bool.

pub fn compile(template: String) -> Result(Template, Error)

compile prepares a template for future application, to avoid the overhead of scanning and parsing a template from scratch every time. Prefer this when speed is important.

Examples

let template_source = "No. I am your {{relative}}."
pomade.compile(template_source)
// -> Ok(Template)
pub fn compile_and_render(
  template: String,
  data: Value,
  partials: dict.Dict(String, String),
) -> Result(string_tree.StringTree, Error)

compile_and_render is a convenience function that compiles and expands a template source string in a single step. Useful for one-off or infrequent rendering of a template (e.g. in batch programs, etc). If you plan on rendering a template repeatedly, then pre-compile it (with compile) and keep that value in memory for later rendering (with render).

Examples

Basic usage:

let template = "Hello, {{target}}!"
let data = pomade.dict(dict.from_list([#("target", pomade.string("world"))]))
pomade.compile_and_render(template, data, dict.new())
// -> Ok(StringTree)
// -> "Hello, world!"

With partials:

let template = "Fly, you {{>other_template}}!"
let data =
  pomade.dict(dict.from_list([#("adjective", pomade.string("fools"))]))
let partials = dict.from_list([#("other_template", "{{adjective}}")])
pomade.compile_and_render(template, data, partials)
// -> Ok(StringTree)
// -> "Fly, you fools!"
pub fn compile_and_render_string(
  template: String,
  data: Value,
  partials: dict.Dict(String, String),
) -> Result(String, Error)

compile_and_render_string is like compile_and_render, but it returns a String instead of a StringTree. Internally, compile_and_render_string calls compile_and_render, and then converts the result, so some allocations are implied. Because of this, prefer compile_and_render whenever possible, if your target API permits it.

pub fn dict(value: dict.Dict(String, Value)) -> Value

dict creates a Value from a Dict(String, Value).

pub fn float(value: Float) -> Value

float creates a Value from a Float.

pub fn int(value: Int) -> Value

int creates a Value from an Int.

pub fn list(value: List(Value)) -> Value

list creates a Value from a List(Value).

pub fn render(
  template: Template,
  data: Value,
  partials: dict.Dict(String, String),
) -> Result(string_tree.StringTree, Error)

render takes a pre-compiled template and applies it to the supplied data and partials.

Examples

let data = pomade.dict(dict.from_list([#("relative", pomade.string("father"))]))
pomade.render(template, data, dict.new())
// -> Ok(StringTree)
// -> "No. I am your father."
pub fn render_string(
  template: Template,
  data: Value,
  partials: dict.Dict(String, String),
) -> Result(String, Error)

render_string is like render, but it returns a String instead of a StringTree. Internally, render_string calls render, and then converts the result, so some allocations are implied. Because of this, prefer render whenever possible, if your target API permits it.

pub fn string(value: String) -> Value

string creates a Value from a String.

✨ Search Document