The `template` Data Type in ThingsDB
DEV Community

The template Data Type in ThingsDB

ThingsDB has long supported template strings using backtick syntax (`Hello {name}!`). While convenient for inline string interpolation, template strings are evaluated immediately at runtime, which means you couldn't store them in a collection, attach them to a typed model, or render them repeatedly across different contexts without manual string building.

With the addition of the new template data type, templates are now first-class values in ThingsDB. You can validate a template once, store it in your collection, enforce it in custom types, and render it on demand against any context.

Creating and Rendering Templates

You create a template using the template() function. ThingsDB parses and validates the template syntax immediately upon creation - catching unclosed braces or invalid expressions up front rather than waiting until render time:

tpl = template('Hello {name}, you have {count} unread messages.');

Once created, you can render the template whenever you need it using .render():

// Render using variables from the current context
name = 'Alice';
count = 3;
tpl.render(); // -> "Hello Alice, you have 3 unread messages."

// Or pass a thing (or typed instance) as an explicit context
tpl.render({name: 'Bob', count: 5}); // -> "Hello Bob, you have 5 unread messages."

Because .render() accepts any thing whose properties are valid variable names, you can pass plain objects or typed instances directly without unpacking their fields first.

First-Class Type Support

Because template is a built-in data type, it integrates with the rest of ThingsDB's type system:

  • Custom Types & Auto-Conversion: Use 'template' (or 'template?') in set_type() definitions. When constructing or updating an instance via Type({...}) or .assign({...}), plain strings are automatically converted and validated into template instances:
set_type('Notification', {title: 'str', body: 'template',});

// String is automatically validated and converted to a template
n = Notification({title: 'Welcome', body: 'Hi {user.name}, welcome to {org}!',});

n.body.render({user: {name: 'Charlie'}, org: 'ThingsDB',}); // -> "Hi Charlie, welcome to ThingsDB!"
  • Built-in Helpers & Serialization:
    • is_template(val) checks whether a value is a template.
    • tpl.len() returns the length of the underlying template string.
    • str(tpl) returns the raw template string, and bool(tpl) evaluates to false for an empty template (template('')) and true otherwise.
    • When a template is returned directly from a query without calling .render(), it serializes cleanly as its raw template string.

Safety Built In

Templates are designed to be safe to store and execute:

  • Up-front Syntax Validation: Malformed expressions raise a syntax_err when template(...) is called or when a string is auto-converted into a typed property.
  • Recursion Protection: If a template attempts to render itself recursively, ThingsDB catches it immediately and raises an operation_err.
  • Context Validation: Passing a thing with keys that cannot be represented as valid ThingsDB variable names (such as an empty key "" or keys with spaces/hyphens) raises a value_err before evaluation begins.

Learn More

Check out the documentation for full details and examples: The template data type, template(), is_template(), template.render(), template.len()

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.