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:
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 <. 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.
input = { name = "Ada", items = { "Flyers", "Cards" }, note = "<b>Rush</b>" }Steps
- Return
greeting:"Dear {{name}},"filled in frominput. - Add
items: each ofinput.itemswritten as- itemfollowed by a new line. - Add
safe:"{{note}}"filled in frominput, 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.