Marcel van Lohuizen | 3e592b4 | 2019-01-11 20:31:29 +0100 | [diff] [blame^] | 1 | [TOC](Readme.md) [Prev](lists.md) [Next](instances.md) |
| 2 | |
| 3 | _Types ~~and~~ are Values_ |
| 4 | |
| 5 | # Templates |
| 6 | |
| 7 | One of CUE's most powerful features is templates. |
| 8 | A template defines a value to be unified which each field of a struct. |
| 9 | |
| 10 | The template's identifier (in angular brackets) is bound to name of each |
| 11 | of its sibling fields and is visible within the template value |
| 12 | that is unified with each of the siblings. |
| 13 | |
| 14 | ``` |
| 15 | // The following struct is unified with all elements in job. |
| 16 | // The name of each element is bound to Name and visible in the struct. |
| 17 | job <Name>: { |
| 18 | name: Name |
| 19 | replicas: 1 | uint |
| 20 | command: string |
| 21 | } |
| 22 | |
| 23 | job list command: "ls" |
| 24 | |
| 25 | job nginx: { |
| 26 | command: "nginx" |
| 27 | replicas: 2 |
| 28 | } |
| 29 | ``` |
| 30 | |
| 31 | ``` |
| 32 | job: { |
| 33 | list: { |
| 34 | name: "list" |
| 35 | replicas: 1 |
| 36 | command: "ls" |
| 37 | } |
| 38 | nginx: { |
| 39 | name: "nginx" |
| 40 | replicas: 2 |
| 41 | command: "nginx" |
| 42 | } |
| 43 | } |
| 44 | ``` |