Skip to content

Jinja2 templates reference ​

This page lists every field and option. The overview shows what it does and a first example.

kdeps uses Jinja2-compatible templates (with gonja). This is the one template system for project scaffolding and for runtime YAML preprocessing. Preprocessing runs for every workflow file and every resource file.

YAML preprocessing ​

Before kdeps parses the YAML, Jinja2 preprocesses each workflow and resource YAML file. With this, you can:

  • Conditionally include or exclude sections based on environment variables
  • Inject environment values directly into YAML at parse time
  • Remove comments before kdeps parses the file
yaml
# workflow.yaml
apiVersion: kdeps.io/v1
kind: Workflow
metadata:
  name: my-api
  version: "1.0.0"
  targetActionId: response
deploy:
  apiServer:
{% if env.PORT %}
    portNum: {{ env.PORT | int }}
{% else %}
    portNum: 8080
{% endif %}

Auto-protection of runtime API calls ​

Before Jinja2 renders the file, kdeps puts each runtime API call in {% raw %}...{% endraw %}. You do not add raw blocks yourself. The runtime API calls are: {{ get(...) }}, {{ set(...) }}, {{ info(...) }}, {{ input(...) }}, {{ output(...) }}, {{ file(...) }}, {{ item(...) }}, {{ loop(...) }}, {{ session(...) }}, {{ json(...) }}, {{ safe(...) }}, {{ debug(...) }}, {{ default(...) }}.

Jinja2 evaluates static expressions like {{ env.PORT }} as usual, because they do not start with a kdeps API function name.

yaml
# resource.yaml - no {% raw %} needed
{% if env.ENABLE_HTTP == 'true' %}
  component:
    name: http_request
    with:
      method: GET
      url: "{{ get('url') }}"
      headers:
        X-Request-ID: "{{ info('request_id') }}"
{% endif %}

Available context variables ​

VariableTypeDescription
envmap[string]stringAll current process environment variables

Template syntax ​

Variables ​

jinja
port: {{ env.PORT | int }}
name: {{ env.SERVICE_NAME | default('my-service') }}

Conditionals ​

jinja
{% if env.DEBUG == 'true' %}
  logLevel: debug
{% else %}
  logLevel: info
{% endif %}

Comments ​

jinja
{# This comment is stripped before parsing #}

Whitespace control ​

Use - to trim surrounding whitespace:

jinja
{%- if env.CERT_FILE %}
  certFile: {{ env.CERT_FILE }}
{%- endif %}

Scaffolding templates ​

Project scaffolding templates (used by kdeps new) also use Jinja2 with .j2 file extensions. Variables available in scaffolding templates:

VariableTypeDescription
namestringProject name
descriptionstringProject description
versionstringVersion string
portintAPI server port
resources[]stringEnabled resource types

See also ​

kdeps has the Apache 2.0 License.