Skip to main content

Overview

SDF supports macro processing with Jinja.
  • Jinja Files - SDF requires macro definitions to be in .jinja files
  • Preprocessor configuration - SDF Requires a workspace to turn on the jinja preprocessor
  • SDF provides some built in functions

Getting Started With Jinja

Let’s explore some of the capabilities unlocked by using jinja in a minimal SDF workspace.
1

Create a new SDF Workspace

Create a new SDF workspace. sdf new jinjex && cd jinjex
2

Configure Your Workspace to Use Jinja

Add a new preprocessor property to the workspace block in your workspace.sdf.yml
workspace.sdf.yml
Congratulations! Your workspace is now configured to run with Jinja.
3

Add a Jinja Expression to main.sql

Replace the contents of /models/main.sql with a short jinja expression like:
This expression creates a table with four columns.Compile To see the schema, run:sdf compile main
Run To execute the SQL statement using SDF as the database target, simply run:sdf run main
4

Creating a New Jinja Function

Let’s create a new function. This function will be accessible globally in your workspace. To start, create a new folder macros and add a new file macros.jinja to the folder.Add the new directory to your includes in the workspace.
workspace.sdf.yml
In your macros.jinja file add a new function which we’ll call switch_schema
macros.jinja
Macros by default inherit their namespace from the Workspace name, in this case jinjex To reference any macro defined in your workspace, specify it with the namespace like jinjex.<function>(params)Check that the function is included by running sdf compile. The output should be all green.
5

Using the Jinja function

It’s time to reference the function. Let’s create a new SQL file, child.sql in the /models folder.
models/child.sql
We are invoking the fucntion jinjex.switch_schema, and creating a new table, in that new schema.Let’s verify that we are now compiling two tables with sdf compile --show all.We can also easily confirm the lineage for our new table - jinjex.private.child by running sdf lineage jinjex.private.child
Or, if we wanted to execute these SQL statements, you simply run sdf run
For additional resources on effectively using macros, please see SDF docs on: