Learn Lua · Numbers and text · Lesson 4 of 4

Templates

A template is text with gaps. template.render fills {{name}} gaps from a table. A {{#list}}…{{/list}} section repeats for each item, and {{.}} is the item itself:

LUA
template.render("Dear {{name}},", { name = "Ada" })                --> "Dear Ada,"
template.render("{{#items}}[{{.}}]{{/items}}", { items = { "a", "b" } })  --> "[a][b]"

{{name}} escapes characters that mean something in HTML, so < becomes &lt;. That keeps emails and web pages built from customer data safe.

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = { name = "Ada", items = { "Flyers", "Cards" }, note = "<b>Rush</b>" }

Steps

  1. Return greeting: "Dear {{name}}," filled in from input.
  2. Add items: each of input.items written as - item followed by a new line.
  3. Add safe: "{{note}}" filled in from input, so HTML in the note is escaped.
Show a hint

Pass input as the data for every template. A new line in a Lua string is \n.