Skip to content

Jinja2 templates ​

kdeps uses Jinja2-compatible templates (via gonja) as the unified template system for both project scaffolding and runtime YAML preprocessing. Preprocessing runs for every workflow and resource file in both modes.

YAML preprocessing ​

Every workflow and resource YAML file is preprocessed through Jinja2 before YAML parsing. This lets you:

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

Auto-protection of runtime API calls ​

kdeps automatically wraps all runtime API function calls ({{ get(...) }}, {{ set(...) }}, {{ info(...) }}, {{ input(...) }}, {{ output(...) }}, {{ file(...) }}, {{ item(...) }}, {{ loop(...) }}, {{ session(...) }}, {{ json(...) }}, {{ safe(...) }}, {{ debug(...) }}, {{ default(...) }}) in {% raw %}...{% endraw %} before Jinja2 renders the file. You do not need to add raw blocks manually.

Static Jinja2 expressions like {{ env.PORT }} are evaluated normally 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
settings:
{%- 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 ​

Released under the Apache 2.0 License.