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…
The `template` data type in ThingsDB is a recent addition that provides first-class support for template strings. These templates can be validated, stored, and rendered efficiently. To create a template, the `template()` function is used, which parses and validates the template syntax immediately upon creation, catching errors like unclosed braces or invalid expressions upfront.
The rendered template can then be generated on demand using the `.render()` method, which accepts any thing whose properties are valid variable names, including plain objects or typed instances. This integration with ThingsDB's type system allows for automatic conversion and validation of plain strings into templates when defining custom types.
The `template` type also comes with built-in helper functions such as `is_template()`, `template.len()`, and `str(tpl)`, providing additional functionality for working with templates. Safety features include up-front syntax validation, recursion protection, and context validation to prevent malformed or unsafe templates from being executed.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to