Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ward.template

MiniJinja

MiniJinja is a fast, Rust-native Jinja2-like template engine.

local tpl = require("ward.template.minijinja")

minijinja.render(template, context, opts?) -> string

Render a template string using the given context (Lua table).

local out = tpl.render("Hello {{ user.name }}!", {
  user = { name = "Ward" }
})
print(out) -- Hello Ward!

minijinja.render_async(template, context, opts?) -> string

Same as render, but runs the render on a blocking thread so it will not block the async runtime.

local out = tpl.render_async("{{ n }}", { n = 42 })
print(out)

minijinja.render_file(path, context, opts?) -> string

Read a file from path and render its contents as a template.

local out = tpl.render_file("./hello.tmpl", { name = "world" })
print(out)

minijinja.render_file_async(path, context, opts?) -> string

Same as render_file, but runs on a blocking thread.

Options

All functions accept an optional opts table:

  • undefined: one of "strict", "lenient", "chainable" (default: "strict")
  • trim_blocks: boolean (default: false)
  • lstrip_blocks: boolean (default: false)
  • keep_trailing_newline: boolean (default: false)
  • auto_escape: boolean (default: false)
  • loader: table configuring {% include %} / {% import %} resolution
    • paths: array of strings; additional search paths for templates

Example:

local out = tpl.render_file("./templates/main.j2", {
  title = "Hello",
  items = { "a", "b", "c" },
}, {
  undefined = "strict",
  trim_blocks = true,
  lstrip_blocks = true,
  loader = {
    paths = { "./templates" },
  },
})
print(out)