Skip to content

Render functions in the MiniJinja 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 minijinja cookbook / technical-details examples.
-- Small, deterministic, idempotent (CREATE OR REPLACE).

-- "Generate HTML email per row" (cookbook) and "What you can do with one
-- query" (technical-details).
CREATE OR REPLACE TABLE users (
    email         TEXT,
    name          TEXT,
    unread_count  INTEGER
);
INSERT INTO users VALUES
    ('[email protected]', 'Alice', 3),
    ('[email protected]',   'Bob',   0),
    ('[email protected]', 'Carol', 12);

-- "Generate config files / IaC" (cookbook).
CREATE OR REPLACE TABLE virtual_hosts (
    hostname  TEXT,
    upstream  TEXT
);
INSERT INTO virtual_hosts VALUES
    ('app.example.com', '10.0.0.10:8080'),
    ('api.example.com', '10.0.0.20:9000');

Function category

Render

2 functions

minijinja_render

Scalar function Render
Signature
minijinja_render( template: VARCHAR, autoescape := BOOLEAN = true, autoescape_extensions := VARCHAR[] = [], template_path := VARCHAR = '', undefined_behavior := VARCHAR = 'lenient' ) → VARCHAR
Arguments
Argument template Type VARCHAR Mode Positional Description Template source (or a template name when template_path is set).
Argument autoescape Type BOOLEAN Mode Named Description Default: true HTML-escape interpolated values (B&O becomes B&O). Pass false for plain text, configs, SQL or JSON output.
Argument template_path Type VARCHAR Mode Named Description Default: '' Directory (or a file, whose directory is used) to load templates from. When set, template is the name of a template file there, which enables {% include %} / {% extends %}. Empty means inline templates only.
Argument undefined_behavior Type VARCHAR Mode Named Description Default: 'lenient' How undefined variables are treated: 'lenient' renders a missing variable as empty but errors on attribute access through it, 'chainable' also renders attribute access on undefined values as empty, 'semi_strict' and 'strict' raise errors. Any other value is a binder error.
Argument autoescape_extensions Type VARCHAR[] Mode Named Description Default: [] When non-empty, HTML autoescape applies only to templates whose name ends with one of these suffixes (for example ['.html']), overriding autoescape — useful with template_path. Must not contain NULL elements.
Description
1 Render without context
SELECT minijinja_render('Hello World!');
2 Chainable undefined values render empty
SELECT minijinja_render('Hi {{ user.name }}!', undefined_behavior := 'chainable');

minijinja_render_with_context

Scalar function Render
Signature
minijinja_render_with_context( template: VARCHAR, context: JSON, autoescape := BOOLEAN = true, autoescape_extensions := VARCHAR[] = [], template_path := VARCHAR = '', undefined_behavior := VARCHAR = 'lenient' ) → 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 JSON context the template is rendered against — a literal, a JSON column, or json_object(...) built from columns.
Argument autoescape Type BOOLEAN Mode Named Description Default: true HTML-escape interpolated values (B&O becomes B&O). Pass false for plain text, configs, SQL or JSON output.
Argument template_path Type VARCHAR Mode Named Description Default: '' Directory (or a file, whose directory is used) to load templates from. When set, template is the name of a template file there, which enables {% include %} / {% extends %}. Empty means inline templates only.
Argument undefined_behavior Type VARCHAR Mode Named Description Default: 'lenient' How undefined variables are treated: 'lenient' renders a missing variable as empty but errors on attribute access through it, 'chainable' also renders attribute access on undefined values as empty, 'semi_strict' and 'strict' raise errors. Any other value is a binder error.
Argument autoescape_extensions Type VARCHAR[] Mode Named Description Default: [] When non-empty, HTML autoescape applies only to templates whose name ends with one of these suffixes (for example ['.html']), overriding autoescape — useful with template_path. Must not contain NULL elements.
Description
1 Basic substitution
SELECT minijinja_render_with_context('{{ foo }}', '{"foo":"bar"}');
2 Nested fields
SELECT minijinja_render_with_context('Hello {{ user.name }}, {{ user.messages }} new', '{"user":{"name":"Alice","messages":5}}');
3 For loop with comma separator
SELECT minijinja_render_with_context('{% for x in xs %}{{ x }}{% if not loop.last %}, {% endif %}{% endfor %}', '{"xs":["A","B","C"]}');
4 Disable autoescape for raw text
SELECT minijinja_render_with_context('{{ v }}', '{"v":"B&O"}', autoescape := false);