Template Engine

Overview

Compozy's YAML template engine enables dynamic configuration by combining standard YAML syntax with Go template expressions. This allows you to create flexible, reusable configurations that adapt to different contexts and data sources.

Core Capabilities

Dynamic Values

Inject data from workflow context, environment variables, and task outputs

Resource References

Reference other resources using `$ref` and `$use` directives

Data Transformations

Apply Sprig functions and custom filters to modify data

Conditional Logic

Create routing and decision-making logic within templates

Template Expression Syntax

Template expressions use Go template syntax wrapped in double curly braces. Here are the essential patterns:

Basic Value Injection

Access data from your workflow context:

# Access workflow input
city: "{{ .workflow.input.city }}"

# Access environment variables
api_key: "{{ .env.OPENAI_API_KEY }}"

# Access task outputs
weather: "{{ .tasks.weather_check.output.temperature }}"

When to use: Direct access to known data sources without transformation.

Common Use Cases

Dynamic Task Configuration

Configure tasks based on runtime data:

- id: api_call
  type: basic
  $use: tool(local::tools.#(id=="http_request"))
  with:
    url: "{{ .env.API_BASE_URL }}/users/{{ .workflow.input.user_id }}"
    method: "{{ .workflow.input.method | default 'GET' }}"
    headers:
      Authorization: "Bearer {{ .env.API_TOKEN }}"

Use case: When you need to configure tasks with different URLs, parameters, or settings based on input data.

Template Processing

The template engine processes your YAML configurations in 5 phases:

1

Parse Phase

YAML structure is parsed and template expressions are identified

The engine identifies template expressions marked with {{ }} and prepares them for processing.

2

Context Resolution

Context data is gathered from workflow state and environment

Data from workflow input, task outputs, and environment variables is collected into a context map.

3

Template Rendering

Go templates are executed with the context data

Template expressions are evaluated using the Go template engine with Sprig functions.

4

Type Conversion

Results are converted to appropriate types

String results are converted to numbers, booleans, or objects as needed.

5

Reference Resolution

Resource references are resolved to actual resources

$ref and $use directives are resolved to actual schemas, tools, and agents.

Security Features

Available Security Functions

htmlEscape

Escapes HTML content to prevent XSS attacks

htmlAttrEscape

Escapes HTML attribute values safely

jsEscape

Escapes JavaScript strings securely

Security Best Practice

# ❌ DANGEROUS: Direct user input
dangerous_output: "{{ .user_input }}"

# ✅ SAFE: Properly escaped user input
safe_output: "{{ .user_input | htmlEscape }}"

References

Build your template expertise progressively: