Data Binding and Templating
Relevant source files
The following files were used as context for generating this wiki page:- packages/string-templates/src/index.ts
- packages/builder/src/dataBinding.js
- packages/client/src/utils/enrichDataBinding.js
- packages/types/src/ui/bindings/binding.ts
- packages/string-templates/README.md
- packages/builder/src/components/backend/DataTable/formula.js
- packages/builder/src/components/common/bindings/CodeEditorField.svelte
The Data Binding and Templating system provides a common mechanism for dynamically inserting data and logic into strings and object properties across the Budibase platform, including the server, client, and builder. It is built upon the Handlebars.js templating engine, extended with a rich set of custom helpers to facilitate complex data manipulation, formatting, and conditional logic.
This system is fundamental to making Budibase applications dynamic. It allows developers to bind component properties to data sources, use formulas in tables, and create dynamic content in automations. The core functionality is encapsulated within the @budibase/string-templates package, which is used by both backend and frontend packages.
Core Templating Engine: @budibase/string-templates
The @budibase/string-templates package is the heart of the templating system. It wraps Handlebars and registers a suite of helpers to provide extended functionality beyond basic variable substitution.
Sources: packages/string-templates/README.md, packages/string-templates/src/index.ts
Template Formats
The system supports two primary Handlebars-based template formats:
-
Single Statement: A simple substitution, often combined with helpers.
Hello I'm building a {{uppercase adjective}} string with Handlebars!Given a context
{ "adjective": "cool" }, this produces:Hello I'm building a COOL string with Handlebars!. -
Conditional Block: Uses block helpers to insert content based on logic.
Hello I'm building a {{#gte score "50"}}Great{{else}}Bad{{/gte}} string with Handlebars!This will output "Great" if
scoreis 50 or more, and "Bad" otherwise.
Sources: packages/string-templates/README.md:39-51
Core Processing Functions
The package exposes several key functions for processing templates.
| Function | Signature | Description |
|---|---|---|
processString | async (string, object) | Asynchronously processes a single template string against a context object. |
processObject | async (object, object) | Asynchronously and recursively processes all string values within an object. |
processStringSync | (string, object) | A synchronous, reduced-functionality version of processString. |
processObjectSync | (object, object) | A synchronous, reduced-functionality version of processObject. Used in the builder UI for real-time previews. |
isValid | (string) | Checks if a given string is a valid template. |
makePropSafe | (string) | Wraps a property name in [] if it contains characters that are invalid in Handlebars paths (e.g., spaces). |
Sources: packages/string-templates/README.md:55-70, packages/string-templates/src/index.ts:182-255
The following diagram illustrates the general flow of string processing.
This diagram shows the high-level steps involved when the processString function is called.
Handlebars Helpers
A wide range of helpers are available, grouped into categories. These helpers provide functionality for math operations, array manipulation, string formatting, conditional comparisons, and more.
| Category | Description | Examples |
|---|---|---|
| Math | Perform mathematical calculations. | add, subtract, avg, round |
| Array | Manipulate and iterate over arrays. | first, last, join, forEach |
| Number | Format numbers for display. | addCommas, bytes, toPrecision |
| URL | Encode or manipulate URL strings. | encodeURI, stripProtocol |
| String | Manipulate and format strings. | uppercase, camelcase, ellipsis |
| Comparison | Perform logical comparisons for conditional blocks. | eq, gt, lt, and, or, not |
| Object | Access or manipulate object properties. | get, JSONparse |
| Regex | Perform regular expression tests. | test |
| Date | Format dates and times using Moment.js syntax. | date |
Sources: packages/string-templates/README.md:8-37
Binding Formats and Conversion
Within Budibase, bindings have two representations: a "readable" format for the UI and a "runtime" format for execution. The system provides utilities to convert between these. JavaScript bindings are also supported for more complex logic.
Readable vs. Runtime Bindings
- Readable Binding: A user-friendly, dot-notation path used in the builder UI (e.g.,
My Table.0.Name). - Runtime Binding: The Handlebars-compatible format that is actually executed (e.g.,
[My Table].0.[Name]). Notice the use of[]to handle property names with spaces.
The packages/builder/src/dataBinding.js file contains the logic for converting between these formats.
runtimeToReadableBinding(bindings, value): Converts a runtime string to its readable equivalent.readableToRuntimeBinding(bindings, value): Converts a readable string to its runtime equivalent.
Sources: packages/builder/src/dataBinding.js, packages/builder/src/components/backend/DataTable/formula.js:68-72
JavaScript Bindings
For more complex logic that cannot be expressed with Handlebars alone, Budibase supports JavaScript bindings. These are wrapped in a specific format to be identified and executed.
encodeJSBinding(js): Wraps a JavaScript string inreturn ( ... ).decodeJSBinding(string): Extracts the raw JavaScript from the wrapped string.
These are used extensively in the Code Editor for component settings.
Sources: packages/string-templates/src/index.ts:119-130, packages/builder/src/components/common/bindings/CodeEditorField.svelte:6-8
Binding Generation
In the Budibase builder, the list of available bindings is dynamically generated based on the application's context, such as data sources and their schemas.
The getBindings function in packages/builder/src/components/backend/DataTable/formula.js is a key example. It recursively traverses a table's schema to generate a list of bindable fields.
- It iterates through each column in a table schema.
- For relationship fields (
FieldType.LINK), it recursively calls itself on the related table, up to a maximum depth (MAX_DEPTH = 1). - It constructs the
label,runtimeBinding, andreadableBindingfor each field. - It skips certain field types that are not suitable for binding, such as
LONGFORMorATTACHMENTS.
Sources: packages/builder/src/components/backend/DataTable/formula.js:29-98
The following diagram shows the process of generating bindings from a table schema.
This flowchart illustrates the logic within the getBindings function for creating a list of available data bindings from a table structure.
Binding Data Structures
The core data structures for bindings are defined in packages/types/src/ui/bindings/binding.ts.
| Interface | Property | Type | Description |
|---|---|---|---|
Binding | label | string | The display name for the binding. |
type | string | The data type of the binding (e.g., "String", "Number"). | |
category | string | The group this binding belongs to (e.g., "Table Fields"). | |
runtimeBinding | string | The Handlebars-compatible binding string. | |
readableBinding | string | The dot-notation binding string for the UI. | |
EnrichedBinding | (extends Binding) | ||
value | any | The evaluated value of the binding in the current context. | |
valueHTML | string | An HTML-highlighted version of the evaluated value. |
Sources: packages/types/src/ui/bindings/binding.ts:1-21
This class diagram shows the relationship between Binding and EnrichedBinding.
This diagram shows that EnrichedBinding inherits all properties from Binding and adds properties for the evaluated value.
Client-Side Binding Enrichment
In the Budibase builder, particularly in the CodeEditorField component, bindings are "enriched" with their live evaluated values to provide a real-time preview for the developer.
This process involves:
- The
CodeEditorFieldcomponent receives a list of availablebindings. - It uses the
processObjectSyncfunction from@budibase/string-templatesto synchronously evaluate each binding'sruntimeBindingagainst the current applicationcontext. - The evaluated
valueand a syntax-highlightedvalueHTMLstring are added to each binding object, creating anEnrichedBinding. - These enriched bindings are then passed to the
CodeEditor's autocompletion menu to display the live preview.
Sources: packages/builder/src/components/common/bindings/CodeEditorField.svelte:70-91
The sequence diagram below illustrates this enrichment process.
This diagram shows how the CodeEditorField component uses the string templating engine to evaluate bindings and provide live data previews in the UI.
Conclusion
The Data Binding and Templating system is a cornerstone of Budibase's dynamic capabilities. By leveraging Handlebars and a custom set of helpers, it provides a powerful and consistent API for data manipulation and substitution across the entire platform. The clear separation between readable UI bindings and executable runtime bindings, combined with client-side enrichment for live previews, creates a developer-friendly experience for building complex, data-driven applications.