Dashboard theming and templating
Dashboard theming separates reusable visual choices from dashboard data and layout. A theme supplies named design tokens, while a frontend template supplies the shared page and widget presentation that uses those tokens. Query templates are a separate concern: they turn dashboard filter and viewer values into executable queries before data is fetched. This page complements Dashboard domain, Dashboard query and semantic model, and Dashboard rendering and export.
Presentation model
The system builds a dashboard view from three related inputs: the dashboard definition, the selected visual template, and the data returned for each widget. The template controls the shared presentation, while the dashboard definition supplies the content and the query layer supplies the data.
flowchart LR
D[Dashboard definition] --> V[Dashboard view]
T[Selected theme tokens] --> V
C[Shared template components] --> V
F[Filters and viewer context] --> Q[Rendered widget queries]
Q --> W[Widget data]
W --> V
The word template has two meanings in this area. A dashboard template is a reusable visual definition. A query template is a text expression that is rendered with runtime values. They meet in the dashboard view, but they solve different problems.
References: frontend/…/template.ts• L105-119 pkg/…/template.go• L13-40 pkg/…/api.go• L254-299
Theme registry
The theme registry is the shared catalog of visual token sets for server mode and static builds. It starts with the built-in light and dark Bruin themes, then adds project themes or a theme file selected on the command line.
Theme sources
The built-in themes are bruin and bruin-dark. A project can add YAML themes with a name and a tokens map. The project theme directory is normally themes/ at the project root. If that directory is not present, the dashboard directory's themes/ directory is used.
The --template option selects a theme by name or points to one YAML file. A file path is loaded as the active named theme. Themes in the project directory are also made available during startup when the directory can be found.
| Source | Role |
|---|---|
| Built-in registry | Provides the default light and dark visual definitions. |
| Project theme directory | Adds named reusable YAML themes. |
--template file path | Adds and selects one theme file directly. |
References: pkg/…/registry.go• L10-28 pkg/…/theme.go• L11-38 pkg/…/project.go• L15-53 pkg/…/server.go• L103-122
Token inheritance
User themes can declare extends to reuse a known theme. The child keeps the base token values and replaces only the values that it defines. This lets a project change its brand colors without copying the full shared palette.
An unknown base theme prevents that directory theme from loading. A theme file selected directly is loaded as a named theme, but the direct-load path does not apply directory inheritance. A directly selected theme that needs inherited values should therefore contain the token values it needs.
References: pkg/…/registry.go• L26-69 testdata/…/corporate.yml• L1-9 pkg/…/server.go• L106-120
Template resolution
The frontend resolves the server's selected name into a complete dashboard template. A complete template combines tokens with the shared component set for the dashboard list, dashboard page, filters, rows, widget frame, and widget types.
Built-in names select their matching visual definition. A project YAML theme changes the token set but does not provide a new frontend component set. If the name is not known to the frontend, it uses the Bruin component set and applies any token values returned by the server.
The server exposes the selected name and, when the name is registered, its merged tokens through the configuration response. Static builds place the same information in the embedded payload. This keeps the first view consistent between a live server and a built dashboard.
References: frontend/…/registry.ts• L4-31 frontend/…/template.ts• L68-119 pkg/…/api.go• L224-252 pkg/…/render.go• L66-90 pkg/…/render.go• L130-152
Visual token application
The active template makes each token available to the dashboard root as a --dac-* style value. Shared layouts and widgets use these values for surfaces, text, borders, status colors, accents, and chart palettes.
Because the token map is shared through the template context, a token change can affect many dashboard surfaces at once. The Bruin filter bar uses the same tokens for its controls, menus, date picker, and validation states, so filters remain part of the dashboard's shared visual language. It also turns filter metadata into the matching control: date ranges support local-time presets and calendar entry, while select menus can show display-only metrics without changing the value sent to the query layer.
Widget renderers receive data through their props and use the active token set for visual choices; they do not choose a separate theme for each widget. Filter changes follow the dashboard filter contract: a date range is applied only when both ends are complete, and a multi-select can preserve a real null value as distinct from the text null.
References: frontend/…/TemplateProvider.tsx• L7-53 frontend/…/index.ts• L13-36 frontend/…/tokens.ts• L1-47 frontend/…/FilterBar.tsx• L12-37 frontend/…/FilterBar.tsx• L310-355 frontend/…/FilterBar.tsx• L623-666 frontend/…/FilterBar.tsx• L731-783 frontend/…/template.ts• L8-24
Query template handling
Query templates make widget data requests depend on the current dashboard state without changing the dashboard definition. The query layer renders a resolved widget query before it sends the query to the data source. The dashboard definition and its query lifecycle are described in Dashboard definition lifecycle.
Runtime values
The query context exposes dashboard filter values under filters. It also exposes the current viewer as bruin.user_email. In a hosted view this value represents the signed-in viewer. In local use it comes from BRUIN_USER_EMAIL, or remains empty when that variable is not set. The same context supports both general text templates and SQL query templates.
The general renderer leaves plain text unchanged. It evaluates expressions and control blocks only when the text uses the supported template markers. A parse or execution error stops construction of the widget query jobs instead of sending invalid text to the next system.
References: pkg/…/template.go• L13-45 pkg/…/api.go• L284-299
SQL template handling
SQL templates use a separate rendering path because query values must be inserted without changing SQL structure. RenderSQL evaluates normal Jinja expressions, conditions, and loops, then accepts text values only inside ordinary single-quoted literals and numeric values in SQL expression positions. It rejects request values that could act as identifiers or SQL fragments.
The SQL path validates filter values before substitution and fails closed when SQL syntax has different meanings across supported dialects. It supports the documented list-value form for IN clauses, but it does not allow included SQL files or transformed output to bypass value capture. These checks keep dashboard filters as data rather than allowing them to become query structure.
The SQL template test suite checks both normal rendering and rejection of unsafe or ambiguous input. This gives query authors a clear boundary: use templates for filter values and simple control flow, not for dynamic table names, columns, clauses, or external SQL fragments.
References: pkg/…/sql.go• L19-68 pkg/…/sql.go• L81-124 pkg/…/sql.go• L126-179 pkg/…/sql.go• L181-383 pkg/…/sql_test.go• L9-127
Query and view flow
The following flow shows how runtime state reaches a widget without mixing query templates with visual templates.
sequenceDiagram
participant U as Viewer
participant V as Dashboard view
participant S as Dashboard service
participant Q as Query renderer
participant B as Data source
U->>V: Select filters
V->>S: Request widget data with filter values
S->>Q: Resolve widget query
Q->>Q: Apply filters and viewer context
Q->>B: Execute rendered query
B-->>S: Return widget data
S-->>V: Return data for the active visual template
The same query rendering step is used when a dashboard is built as a static artifact. The build first creates the theme configuration and resolves the widget jobs, then embeds the resulting configuration and data for the frontend to display. The resulting data then follows the presentation path described in Dashboard rendering and export.
References: frontend/…/DashboardView.tsx• L47-92 frontend/…/client.ts• L165-176 pkg/…/api.go• L254-308 pkg/…/render.go• L83-137
Change checklist
When these files or sections are changed, remember to consider these:
- Keep built-in names aligned across the backend registry, frontend resolver, and configuration payload. A name that exists in only one place can silently fall back to the Bruin visual components.
- Keep token names aligned between YAML themes, frontend token maps, CSS variable consumers, and chart or status color lookups. A renamed token can leave a surface without its intended value.
- Preserve the distinction between a YAML theme and a component template. Adding token loading does not add a new frontend component set.
- Keep inheritance behavior aligned between directory-loaded themes and themes passed as direct files. Direct file selection must carry any values that it cannot inherit.
- Treat query template context names and SQL placement rules as contracts with dashboard definitions. Changes to
filters,bruin.user_email, or accepted SQL positions affect every templated widget query. - Keep SQL template values separate from SQL structure. Dynamic identifiers and fragments need a different product-level design rather than a wider value substitution rule.
- Keep live-server configuration and static-build configuration equivalent so the selected theme and token overrides produce the same presentation.
- Keep filter metadata aligned with the filter controls. Changes to date-range presets, option metrics, or null values can alter both the displayed control and the value passed to query templates.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A theme declares an unknown extends value. | The directory load reports an error and that theme is not available. | Use a built-in theme or a theme that is already registered. |
| A directly selected YAML theme relies on omitted inherited tokens. | The active theme contains only the values loaded from that file. | Include the required token values in the direct file. |
| The selected name is absent from the frontend registry. | The view uses Bruin components and only applies tokens supplied by the server. | Keep selected names and frontend template registrations in sync. |
| A theme file fails schema validation or has no name. | Direct selection fails startup or build; a project-directory load is skipped with a warning. | Validate the YAML theme and provide a non-empty name. |
| A query template has invalid syntax or cannot execute with its runtime values. | Widget query jobs fail before the data source receives the query. | Keep template expressions valid and provide the filter or viewer values they require. |
| A SQL template places a value in an identifier, SQL fragment, unsupported literal, or ambiguous dialect construct. | SQL rendering fails instead of sending a query whose meaning can change by dialect. | Keep request values in quoted value positions or numeric positions supported by RenderSQL. |
| A SQL template includes an external file or transforms captured output through a block. | The renderer rejects the template because the output cannot be checked as a value. | Resolve SQL files before rendering and keep request-controlled output direct. |
bruin.user_email is used in local mode without BRUIN_USER_EMAIL. | The rendered query receives an empty viewer value. | Set the local viewer email when the query depends on it. |
| A date-range filter has only one end or an invalid typed date. | The filter does not send a new complete range to the query layer. | Provide valid start and end dates before relying on the filter value. |
Key files
| File | Why to read it |
|---|---|
| pkg/…/registry.go | Defines the theme catalog, built-in entries, directory loading, and token inheritance. |
| pkg/…/theme.go | Defines the YAML theme shape and the validation boundary for theme files. |
| pkg/…/project.go | Shows how the project theme directory is located. |
| pkg/…/server.go | Shows how live serving loads and selects themes. |
| pkg/…/api.go | Shows the configuration and theme responses and the point where query templates render. |
| pkg/…/render.go | Shows how static builds prepare theme configuration and embed it with dashboard data. |
| frontend/…/registry.ts | Shows how the frontend maps names to reusable visual definitions and handles unknown names. |
| frontend/…/TemplateProvider.tsx | Shows how the active template shares tokens and components with the dashboard view. |
| frontend/…/FilterBar.tsx | Shows how the active visual definition presents filter types and keeps filter values separate from display labels and metrics. |
| frontend/…/template.ts | Defines the high-level contract for reusable dashboard presentation components. |
| pkg/…/template.go | Defines the runtime context and behavior for general query templates. |
| pkg/…/sql.go | Defines the SQL-specific rendering boundary that keeps filter values separate from SQL structure. |
| pkg/…/sql_test.go | Shows the guarantees for SQL template rendering and rejection of unsafe or ambiguous input. |