Render functions in the Tera DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
-- Fixtures for tera cookbook / technical-details / functions.json examples.
-- Small, deterministic, idempotent. Backs snippets that read from `users`
-- (per-row HTML email) and `virtual_hosts` (per-host config).
CREATE OR REPLACE TABLE users (
email TEXT,
name TEXT,
unread_count INTEGER
);
INSERT INTO users VALUES
('[email protected]', 'Alice', 5),
('[email protected]', 'Bob', 0),
('[email protected]', 'Carol', 2);
CREATE OR REPLACE TABLE virtual_hosts (
hostname TEXT,
upstream TEXT
);
INSERT INTO virtual_hosts VALUES
('a.example.com', '10.0.0.1:8080'),
('b.example.com', '10.0.0.2:8080'),
('c.example.com', '10.0.0.3:8080');
Function category
Render
1 functionRender Tera templates with JSON context. Single-function surface; the optional template_path argument lets you load templates from disk by name rather than embedding them inline.
Signature
Arguments
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
template
|
Type
VARCHAR
|
Mode Positional |
Description
Template source, or a template name when template_path is set.
|
Argument
context
|
Type
JSON
|
Mode Positional |
Description
Context the template is rendered against: a JSON object — a literal, a JSON column, json_object(...) built from columns, or any value that casts to JSON such as a struct or a whole row. Arrays, scalars and JSON null are rejected on the DuckDB 2.0 build. Omit it for templates without variables.
|
Argument
autoescape
|
Type
BOOLEAN
|
Mode Named |
Description
Default: true
HTML-escape interpolated values (B&O becomes B&O), in inline templates and in template_path files whatever their extension. Pass false for plain text, configs, SQL or JSON output.
|
Argument
template_path
|
Type
VARCHAR
|
Mode Named |
Description
Default: ''
A template file, or a glob of them (for example './templates/*.html' or './templates/**/*.html'); templates are named by their path relative to the glob's directory. When set, template is a template name, which enables {% extends %} / {% include %} / {% import %}. Empty means inline templates only. On the DuckDB 2.0 build the files are read once per query through DuckDB's file system, so enable_external_access, allowed_directories and allowed_paths apply, and a glob that matches nothing is an error.
|
Argument
autoescape_extensions
|
Type
VARCHAR[]
|
Mode Named |
Description
Default: []
Only used with template_path: when non-empty, file templates are HTML-escaped only if their name ends with one of these suffixes (for example ['.html']). With the default [], autoescape applies to every template. Has no effect when autoescape is false. Inline templates follow autoescape and ignore this list. Must not contain NULL elements.
|
Description
Render a Tera template, optionally with a JSON context. Supports the full Tera vocabulary: variables, filters, conditionals, for loops, autoescape, and inclusion. Options are keyword-only named parameters with defaults — autoescape := false for non-HTML output, template_path := './templates/*.html' to load templates from disk by name. They must be constant expressions; unknown option names or values of the wrong type are rejected when the query is bound, and a NULL argument (including a NULL option) makes the result NULL.
DuckDB 2.0 build: Tera's get_env() function is disabled (it would expose the server's environment variables) unless you SET tera_allow_get_env = true while enable_external_access is on. template_path files are read through DuckDB's file system, so they are refused when enable_external_access is false unless allowed_directories/allowed_paths cover them. The context must be a JSON object (any value that casts to JSON works, including a struct or a whole row). Templates that would overflow the stack — self-including or self-importing templates, recursive macros, or more than 64 levels of nested tags or brackets — are rejected with an error.
SELECT tera_render('{{ foo }}', '{"foo":"bar"}');
SELECT tera_render('Hello {{ user.name }}', '{"user":{"name":"Alice"}}');
SELECT tera_render('{{ user.name }} has {{ tags | length }} tags', {'user': {'name': 'Alice'}, 'tags': ['a', 'b']});
SELECT tera_render('{{ v }}', '{"v":"B&O"}', autoescape := false);
-- browser-unavailable: File-based templates are unavailable in this browser build. Run this example in native DuckDB with your template files.
SELECT tera_render('email.html', '{"name":"Alice"}', template_path := './templates/*.html');