YAML
It is recommended to default to YAML syntax because of its simplicity and readability.Folded and literal strings
Sometimes you might want to use multi-line strings in YAML-based data models, e.g., in parameters such assql or description. It is recommended to use literal
(|) string style in such cases as it preserves line breaks.
Jinja
Please check the Jinja documentation for details on Jinja syntax.Previewing YAML
You can preview the data model code after applying Jinja templates in the Data Model editor by clicking … → Jinja Preview on files that contain Jinja templates in the sidebar.Currently, there’s no way to preview the data model code in YAML after applying
Jinja templates in Cube Core. Please track this issue.
/v1/meta REST (JSON) API endpoint.
Loops
Jinja supports looping over lists and dictionaries. In the following example, we loop over a list of nested properties and generate aLEFT JOIN UNNEST clause for each one: for each one:
Macros
Cube data models also support Jinja macros, which allow you to define reusable snippets of code. You can read more about macros in the Jinja documentation. In the following example, we define a macro calleddimension() which generates
a dimension definition in Cube. This macro is then invoked multiple times to
generate multiple dimensions:
sql
property:
Reusing macros across files
You can define macros in dedicated.jinja files and import them into your
data model files using Jinja’s import statement. This
is useful for sharing common patterns across multiple cubes and views.
Consider the following project structure:
.jinja file under the macros/ directory:
model/macros/common_dimensions.jinja
model/cubes/orders.yml
model/ directory. cents_to_dollars expands to a
single line, so the block scalar alone is enough. A macro that can emit more than one line
also needs the indent filter — see emitting SQL from a
macro.
Escaping unsafe strings
Auto-escaping of unsafe string values in Jinja templates is enabled by default. Substituted values are escaped as JSON strings, so they get wrapped in quotes, potentially breaking YAML syntax. This applies to every substituted value — not only to strings coming from Python, but also to loop variables, macro arguments, and values set in the template itself. You can work around that by using thesafe Jinja
filter with such string values:
safe depends on where the value lands. When a value is the
whole of a YAML value, the quotes are harmless and it compiles as written:
type: {{ type }} renders as type: "sum". As soon as anything is
concatenated with it, the quotes end up inside the line and break it:
{{ name }}_{{ period }} renders as "revenue"_"week", and the model fails
with bad indentation of a mapping entry. Apply safe to every value that is
concatenated with other text:
cube_dbt package.
Emitting SQL from a macro
When a macro takes a SQL expression as an argument, emit it as a literal string (|-) rather than inline. A SQL
expression is arbitrary text, and inline it has to avoid everything YAML reads
as syntax: {CUBE}.amount starts a flow mapping, amount # note truncates at
the comment, and wrapping the whole thing in double quotes only moves the
problem to expressions that contain one, such as {CUBE}."amount".
A block scalar ends at the first line indented less than its opening, so a
multi-line expression also needs the indent
filter — without it, the second line of a CASE expression closes the block
and is read as a mapping key. Set the width to the indentation of the block’s
value line, not to some fixed number: the indent(10) below is 10 because the
macro emits sql: |- at 8 spaces and the value two further in.
Apply indent before safe, not after. indent returns a fresh, unmarked
string, so sql | safe | indent(10) throws the marker away and the SQL
arrives quoted, as "{CUBE}.amount". Marking the result of indent keeps the
expression raw:
Python
Template context
You can use Python to declare functions that can be invoked and variables that can be referenced from within a Jinja template. These functions and variables must be defined inmodel/globals.py file and registered in the TemplateContext instance.
See the
TemplateContext reference for more details.load_data that supposedly loads
data from a remote API endpoint. We will then use the function to generate a data model:
@template.function decorator, we can
call it from within a Jinja template. In the following example, we’ll call the
load_data() function and use the result to generate a data model.
Imports
In themodel/globals.py file (or the cube.py configuration file), you can
import modules from the current directory. In the following example, we import a function
from the utils module and use it to populate a variable in the template context:
model/utils.py
model/globals.py
Dependencies
If you need to use dependencies in your dynamic data model (or yourcube.py
configuration file), you can list them in the requirements.txt file in the root
directory of your Cube deployment. They will be automatically installed with pip on
the startup.
cube package is available out of the box, it doesn’t need to be
listed in requirements.txt.cube_dbt
package useful. It provides a set of utilities that simplify
defining the data model in YAML based on dbt models.
If you need to use dependencies with native extensions, build a custom Docker
image.