Language guide · 01
Variables
Variables are the bridge between your application data and a Knap template. The host supplies the values; the template decides how to present them.
Output a value
Wrap a variable name in double braces. Whitespace inside the braces is optional.
1# {{ title }}23By {{author}}Filters can transform a value before it is written. They run from left to right.
1{{ title | trim | title }}2{{ tags | join:", " }}Value types
Knap accepts unknown application values rather than imposing a schema. Templates commonly work with strings, numbers, booleans, arrays, objects, and nullish values.
stringTitles, content, URLsnumberCounts and measurementsbooleanFeature and state flagsarrayTags, authors, sectionsobjectNested structured datanullMissing or empty valuesAccess nested values
Use dot notation for nested object properties and bracket notation for array items or keys that are easier to express as strings.
1{{ author.name }}2{{ authors[0].name }}3{{ metadata["article:section"] }}Bracket expressions can also use another variable, which is useful when two arrays need to be read in parallel.
1{% for line in transcript %}2{{ timestamps[loop.index0] }} — {{ line }}3{% endfor %}Use human-readable names
Variable output supports names with spaces, so imported column headings can remain readable without preprocessing.
1{{ First name | trim }}2{{ Publication date | date:"YYYY-MM-DD" }}Resolve values asynchronously
If a value is not present in the variables object, the host can load it with resolveVariable. Local values always take precedence.
1const result = await engine.render('{{ remoteValue | upper }}', {2 variables: {},3 context: { documentId: 'example' },4 resolveVariable: async (name, { context }) => {5 if (name === 'remoteValue') {6 return loadValue(context.documentId);7 }8 return undefined;9 },10});Assign a local variable
Use {% set %} to name a literal, expression, or filtered value for the rest of the template.
1{% set slug = title | lower | replace:" ":"-" %}2File: {{ slug }}.mdAssignments are evaluated in order and can be used by later output, conditions, and loops.