Skip to content

Templates

Cohesive Code uses Gonja templates for text rendering. A template receives a single root variable named data.

Process:

ctx.generate("templates/greeting.tpl", "generated/greeting.txt", {
name: "Cohesive Code",
});

Template:

Hello {{ data.name }}!

Output:

Hello Cohesive Code!

Templates should format data. They should not own deep transformation logic, fallback naming, schema walking, or cross-file decisions.

Prefer shaping a stable model in TypeScript:

const operations = Object.entries(spec.paths ?? {}).flatMap(([path, item]) => {
if (!item) return [];
return ["get", "post", "put", "patch", "delete"].flatMap((method) => {
const operation = item[method];
if (!operation) return [];
return [{
method: method.toUpperCase(),
path,
operationId: operation.operationId ?? `${method}_${path.replace(/[^a-z0-9]+/gi, "_")}`,
}];
});
});

Then render with simple loops:

{% for operation in data.operations %}
- `{{ operation.operationId }}` -> `{{ operation.method }} {{ operation.path }}`
{% endfor %}

Template paths resolve relative to ccode_path.

ctx.renderTemplate("templates/readme.tpl", model);
ctx.generate("templates/readme.tpl", "README.generated.md", model);
ctx.accelerate("src/handlers.ts", "templates/handlers.tpl", model);

Pass plain objects, arrays, strings, numbers, booleans, and null-like values. Avoid class instances or values that depend on JavaScript prototypes because the runner converts values before giving them to the template engine.