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.
Templates
Section titled “Templates”One render per row
Section titled “One render per row”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.
One render for the whole table
Section titled “One render for the whole table”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>.
Queries
Section titled “Queries”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 totalFROM "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, andcontext("name").fromQuery(...)keeps it as one value; the rows pass through..maxResponseBytes(n)raises the result’s size limit (1 MiB by default;-1is unlimited).
Scripts
Section titled “Scripts”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.
When to use which
Section titled “When to use which”- 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.
Related
Section titled “Related”- Formula: the language, from loading to exporting
- Formula node: node files and every statement
- Ask AI in a workflow
- Learn by example: every seeded lesson