Templates
Cohesive Code uses Gonja templates for text rendering. A template receives a single root variable named data.
Basic template
Section titled “Basic template”Process:
ctx.generate("templates/greeting.tpl", "generated/greeting.txt", { name: "Cohesive Code",});Template:
Hello {{ data.name }}!Output:
Hello Cohesive Code!Keep templates shallow
Section titled “Keep templates shallow”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 %}Path rules
Section titled “Path rules”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);Data rules
Section titled “Data rules”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.