Skip to content

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 function

tera_render

Scalar function Render
Signature
tera_render( template: VARCHAR, [context: JSON], autoescape := BOOLEAN = true, autoescape_extensions := VARCHAR[] = [], template_path := VARCHAR = '' ) → VARCHAR
Arguments
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
1 Inline variable
SELECT tera_render('{{ foo }}', '{"foo":"bar"}');
2 Nested context
SELECT tera_render('Hello {{ user.name }}', '{"user":{"name":"Alice"}}');
3 Struct as context
SELECT tera_render('{{ user.name }} has {{ tags | length }} tags', {'user': {'name': 'Alice'}, 'tags': ['a', 'b']});
4 Disable autoescape
SELECT tera_render('{{ v }}', '{"v":"B&O"}', autoescape := false);
5 Render a named template file
-- 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');