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?') inset_type()definitions. When constructing or updating an instance viaType({...})or.assign({...}), plain strings are automatically converted and validated intotemplateinstances:
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 atemplate. -
tpl.len()returns the length of the underlying template string. -
str(tpl)returns the raw template string, andbool(tpl)evaluates tofalsefor an empty template (template('')) andtrueotherwise. - When a
templateis 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_errwhentemplate(...)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
thingwith keys that cannot be represented as valid ThingsDB variable names (such as an empty key""or keys with spaces/hyphens) raises avalue_errbefore evaluation begins.
Learn More
Check out the documentation for full details and examples:
原文:https://www.a8q9.cn/en/article/the-template-data-type-in-thingsdb-451ec6.html