Skip to content

Templates, queries, and scripts

Three statements hand the rows to another language and bring the result back as rows. Each is a line in a formula node’s node file, so formula statements can come before and after it:

Statement Language Lesson in Learn the Fontana Formula Language
fromTemplate(text) Jinja 13 Templates
fromQuery(text, language) SQL, JSONata, or JSONPath 09 Query and Script alongside formulas
fromScript(text, language) JavaScript or Python 09 Query and Script alongside formulas

Write the text between backticks so it can span lines.

With eachRow(), the template renders once per row and reads the row as row:

eachRow().fromTemplate(`Dear {{ row.contact }}, invoice {{ row.invoice_id }} for {{ row.amount }} {{ row.ccy }} is due on {{ row.due_date }}.`)
.send("contact", "invoice_id", "amount", "ccy", "due_date")
setProp("reminder", prop("rendered"))

Without .returns, the text lands in one column, rendered. A template that renders a JSON object fills several typed columns at once when you declare them:

eachRow().fromTemplate(`{
"label": {{ row.customer | upper | tojson }},
"size": {% if row.amount >= 2500 %}"LARGE"{% else %}"SMALL"{% endif %}
}`)
.send("customer", "amount")
.returns("label", "text")
.returns("size", "text")

The tojson filter writes a value as JSON, with quotes around text, so the rendered object is always valid.

Without eachRow(), the template reads all rows as rows and their count as numRows. A template that renders a JSON list replaces the rows:

fromTemplate(`[
{% for r in rows %}
{"invoice_id": {{ r.invoice_id | tojson }}, "amount": {{ r.amount }}}{% if not loop.last %},{% endif %}
{% endfor %}
]`)
.send("invoice_id", "amount")

To keep the rows and hold the text as one value, such as an email body, write context("body").fromTemplate(...). See Send and receive email.

.send(name, …) names the columns the template sees. .tables(name, …) lets it read named tables as inputs.<name>.

fromQuery(text, "sql") runs SQL over the rows, which it reads as the table rows. The result replaces the rows:

fromQuery(`SELECT account, ccy, SUM(amount) AS total
FROM "rows"
WHERE flow_type <> 'FEE'
GROUP BY account, ccy`, "sql")
  • .tables(name, …) lets SQL read named tables by their wire handles, for a join.
  • "jsonata" and "jsonpath" read the rows as $; they take no .tables.
  • inputs("name").fromQuery(...) writes the result to a named table, and context("name").fromQuery(...) keeps it as one value; the rows pass through.
  • .maxResponseBytes(n) raises the result’s size limit (1 MiB by default; -1 is unlimited).

fromScript(text, "js") runs a JavaScript function once per row. It receives the row and returns the rows that replace it:

fromScript(`({ row, context }) => {
if (!context.totals) context.totals = {};
const key = row.account + "|" + row.ccy;
context.totals[key] = (context.totals[key] ?? 0) + Number(row.amount);
return { rows: [{ ...row, running_total: context.totals[key] }] };
}`, "js")

Return { rows: [] } to drop the row, one row to replace it, or several rows to fan it out. context is one object for the whole statement, so it carries state from row to row. "python" runs a Python def transform(ctx) instead. .tables(name, …) hands the script named tables in tables.

A column a script adds has no declared type, so formula statements after it read it as any value: convert it with toNumber, parseDate, or setType first.

  • Formula statements (setProp, filter, groupBy) first: they keep cell lineage and styles exact. See Formula.
  • A template when the result is text, such as a message, a report line, or a JSON body for an API.
  • A query when the reshaping is easiest to say in SQL, such as a join or a window.
  • A script for logic that needs a general-purpose language.