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 | |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 7 | <!-- jba: this is not in the spec, aside from the TemplateLabel grammar rule. --> |
| 8 | |
Marcel van Lohuizen | 3e592b4 | 2019-01-11 20:31:29 +0100 | [diff] [blame] | 9 | One of CUE's most powerful features is templates. |
| 10 | A template defines a value to be unified which each field of a struct. |
| 11 | |
| 12 | The template's identifier (in angular brackets) is bound to name of each |
| 13 | of its sibling fields and is visible within the template value |
| 14 | that is unified with each of the siblings. |
| 15 | |
| 16 | ``` |
| 17 | // The following struct is unified with all elements in job. |
| 18 | // The name of each element is bound to Name and visible in the struct. |
| 19 | job <Name>: { |
| 20 | name: Name |
Marcel van Lohuizen | c9b3cb2 | 2019-01-30 11:32:41 +0100 | [diff] [blame] | 21 | replicas: uint | *1 |
Marcel van Lohuizen | 3e592b4 | 2019-01-11 20:31:29 +0100 | [diff] [blame] | 22 | command: string |
| 23 | } |
| 24 | |
| 25 | job list command: "ls" |
| 26 | |
| 27 | job nginx: { |
| 28 | command: "nginx" |
| 29 | replicas: 2 |
| 30 | } |
| 31 | ``` |
| 32 | |
| 33 | ``` |
| 34 | job: { |
| 35 | list: { |
| 36 | name: "list" |
| 37 | replicas: 1 |
| 38 | command: "ls" |
| 39 | } |
| 40 | nginx: { |
| 41 | name: "nginx" |
| 42 | replicas: 2 |
| 43 | command: "nginx" |
| 44 | } |
| 45 | } |
Jonathan Amsterdam | e479038 | 2019-01-20 10:29:29 -0500 | [diff] [blame] | 46 | ``` |