Instructions for modifying Dockerfile templates
All Dockerfiles and Readmes are generated from Cottle templates in
the eng/dockerfile-templates and eng/readme-templates directories respectively.
Cottle syntax
Templating
All text is plain text by default. Text inside double curly braces {{ }} is template code.
Writing just {{value}} emits the evaluated value.
You can also have expressions inside template blocks which do more complicated logic.
Template code can have multiple lines in it, separated by the line ending ^ character.
The final line in a template block should not use the line ending character.
Comments inside a template block start with _ and follow the same line ending rules.
Comments can span multiple lines.
Template data sources
VARIABLES: Variables from manifest.versions.json. Access with VARIABLES["name"].
ARGS: Passed into template via InsertTemplate(...) function. Access with ARGS["name"]
Common functions
- Assign value:
set name to <expression>.
- Assign function
set func(arg1, arg2) to:{{ ... }} defines an inline function; use return <expr> inside.
- String functions:
cat(a, b, ...)
join(list, sep)
split(str, sep)
slice(list, start, end).
- Collections:
len(list),
find(haystack, needle) returns index or -1.
- Ternary expression:
when(condition, valueIfTrue, valueIfFalse).
Conditionals
Boolean expressions:
- Operators:
=, !=, >, <, >=, <=, logical &&, ||, and negation !.
- Equality uses single
= inside expressions (context-specific to Cottle) rather than ==.
If statement:
{{if condition:VALUE}}
Compound conditionals:
{{if ((condition1 && condition2) || condition3):VALUE}}
Multiple conditions:
{{if condition1:VALUE1^
elif condition2:VALUE2^
else:VALUE3}}
After the colon :, all whitespace is treated as literal until the next line ending character ^.
You can also nest template blocks:
{{if condition1:{{if condition2:VALUE}}}}
1---2name: dockerfile-and-readme-templating3description: Modify Cottle templates that generate Dockerfiles and READMEs in dotnet/dotnet-docker.4metadata:5 source: https://skillmd.com/skills/dotnet/dockerfile-and-readme-templating6---78# Instructions for modifying Dockerfile templates910All Dockerfiles and Readmes are generated from [Cottle](https://github.com/r3c/cottle) templates in11the `eng/dockerfile-templates` and `eng/readme-templates` directories respectively.1213## Cottle syntax1415### Templating1617All text is plain text by default. Text inside double curly braces `{{ }}` is template code.18Writing just `{{value}}` emits the evaluated value.19You can also have expressions inside template blocks which do more complicated logic.20Template code can have multiple lines in it, separated by the line ending `^` character.21The final line in a template block should not use the line ending character.22Comments inside a template block start with `_` and follow the same line ending rules.23Comments can span multiple lines.2425### Template data sources2627- `VARIABLES`: Variables from `manifest.versions.json`. Access with `VARIABLES["name"]`.28- `ARGS`: Passed into template via `InsertTemplate(...)` function. Access with `ARGS["name"]`2930### Common functions3132- Assign value: `set name to <expression>`.33- Assign function `set func(arg1, arg2) to:{{ ... }}` defines an inline function; use `return <expr>` inside.34- String functions:35 - `cat(a, b, ...)`36 - `join(list, sep)`37 - `split(str, sep)`38 - `slice(list, start, end)`.39- Collections:40 - `len(list)`,41 - `find(haystack, needle)` returns index or -1.42- Ternary expression: `when(condition, valueIfTrue, valueIfFalse)`.4344### Conditionals4546Boolean expressions:4748- Operators: `=`, `!=`, `>`, `<`, `>=`, `<=`, logical `&&`, `||`, and negation `!`.49- Equality uses single `=` inside expressions (context-specific to Cottle) rather than `==`.5051If statement:5253```cottle54{{if condition:VALUE}}55```5657Compound conditionals:5859```cottle60{{if ((condition1 && condition2) || condition3):VALUE}}61```6263Multiple conditions:6465```cottle66{{if condition1:VALUE1^67elif condition2:VALUE2^68else:VALUE3}}69```7071After the colon `:`, all whitespace is treated as literal until the next line ending character `^`.7273You can also nest template blocks:7475```cottle76{{if condition1:{{if condition2:VALUE}}}}77```