Skip to content

Commit baabecd

Browse files
authored
docs(product): add more examples to Jinja page (#7015)
1 parent 4f31619 commit baabecd

File tree

1 file changed

+56
-30
lines changed
  • docs/docs-new/pages/product/data-modeling/advanced

1 file changed

+56
-30
lines changed

docs/docs-new/pages/product/data-modeling/advanced/jinja.mdx

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,60 @@ your feedback on this feature, please post an issue on GitHub.
1212
</InfoBox>
1313

1414
To use a Jinja template, create a file in your data model folder with the
15-
`.jinja` extension. In the following example, we'll create a file named
16-
`orders.yml.jinja` under the `models/` directory.
15+
`.jinja` extension. For example, a file containing the `orders` cube could be
16+
named `orders.yml.jinja` under the `models/` directory.
1717

1818
## Loops
1919

2020
Jinja supports [looping][jinja-docs-for-loop] over lists and dictionaries. In
21-
the following example, we loop over a list of columns and generate a dimension
22-
for each one:
21+
the following example, we loop over a list of nested properties and generate a
22+
`LEFT JOIN UNNEST` clause for each one: for each one:
2323

2424
```twig
25-
{%- set columns = [
26-
"id",
27-
"status",
28-
"created_at",
29-
"completed_at"
25+
{%- set nested_properties = [
26+
"referrer",
27+
"href",
28+
"host",
29+
"pathname",
30+
"search"
3031
] -%}
3132
3233
cubes:
33-
- name: orders
34+
- name: analytics
3435
sql: >
3536
SELECT
36-
{%- for column in columns %}
37-
{{column}},
38-
{%- endfor %}
39-
SUM(number) AS total_amount
40-
FROM public.orders
41-
GROUP BY 1
37+
{%- for prop in nested_properties %}
38+
{{ prop }}_prop.value AS {{ prop }}
39+
{%- endfor %}
40+
FROM public.events
41+
{%- for prop in nested_properties %}
42+
LEFT JOIN UNNEST(properties) AS {{ prop }}_prop ON {{ prop }}_prop.key = '{{ prop }}'
43+
{%- endfor %}
44+
```
4245

43-
dimensions:
44-
- name: id
45-
sql: id
46-
type: number
47-
primary_key: true
46+
Another useful pattern is to loop over a dictionary of values and generate a
47+
measure for each one, as in the following example:
4848

49-
- name: status
50-
sql: status
51-
type: string
49+
```twig
50+
{%- set metrics = {
51+
"mau": 30,
52+
"wau": 7,
53+
"day": 1
54+
} %}
5255
53-
- name: created_at
54-
sql: created_at
55-
type: time
56+
cubes:
57+
- name: orders
58+
sql_table: public.orders
5659
57-
- name: completed_at
58-
sql: completed_at
59-
type: time
60+
measures:
61+
{%- for name, days in metrics.items() %}
62+
- name: {{ name }}
63+
type: count_distinct
64+
sql: user_id
65+
rolling_window:
66+
trailing: {{ days }} day
67+
offset: start
68+
{% endfor %}
6069
```
6170

6271
## Macros
@@ -91,6 +100,23 @@ cubes:
91100
{{ dimension('completed_at', 'time') }}
92101
```
93102

103+
You could also use macros to generate SQL snippets for use in the `sql`
104+
property:
105+
106+
```twig
107+
{%- macro cents_to_dollars(column_name, precision=2) -%}
108+
({{ column_name }} / 100)::NUMERIC(16, {{ precision }})
109+
{%- endmacro -%}
110+
111+
cubes:
112+
- name: payments
113+
sql: >
114+
SELECT
115+
id AS payment_id,
116+
{{ cents_to_dollars('amount') }} AS amount_usd
117+
FROM app_data.payments
118+
```
119+
94120
## Calling Python functions
95121

96122
You can declare and invoke Python functions from within a Jinja template. This

0 commit comments

Comments
 (0)