Skip to content

Commit

Permalink
docs: Detail how to use Jinja parameters (#20308)
Browse files Browse the repository at this point in the history
We expand the example to show how to use the `from_dttm` and `to_dttm`
Jinja parameters in logic blocks (e.g. `{% if … %}`) and underline when
to use the double braces and when not to.

All this because, well, *one* could lost quite an extensive amount of
time figuring this all out (-_-")

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau-sierra@unipart.io>
  • Loading branch information
EBoisseauSierra authored Jun 8, 2022
1 parent a57ed76 commit 80be1ce
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/docs/installation/sql-templating.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ For example, to add a time range to a virtual dataset, you can write the followi
SELECT * from tbl where dttm_col > '{{ from_dttm }}' and dttm_col < '{{ to_dttm }}'
```

You can also use [Jinja's logic](https://jinja.palletsprojects.com/en/2.11.x/templates/#tests)
to make your query robust to clearing the timerange filter:

```sql
SELECT *
FROM tbl
WHERE (
{% if from_dttm is not none %}
dttm_col > '{{ from_dttm }}' AND
{% endif %}
{% if to_dttm is not none %}
dttm_col < '{{ to_dttm }}' AND
{% endif %}
true
)
```

Note how the Jinja parameters are called within double brackets in the query, and without in the
logic blocks.

To add custom functionality to the Jinja context, you need to overload the default Jinja
context in your environment by defining the `JINJA_CONTEXT_ADDONS` in your superset configuration
(`superset_config.py`). Objects referenced in this dictionary are made available for users to use
Expand Down

0 comments on commit 80be1ce

Please sign in to comment.