blob: bd40c2aa153b14c55b43ca3be06dd8c108a01b1d [file] [log] [blame] [view]
Marcel van Lohuizen3e592b42019-01-11 20:31:29 +01001[TOC](Readme.md) [Prev](lists.md) [Next](instances.md)
2
3_Types ~~and~~ are Values_
4
5# Templates
6
Jonathan Amsterdame4790382019-01-20 10:29:29 -05007<!-- jba: this is not in the spec, aside from the TemplateLabel grammar rule. -->
8
Marcel van Lohuizen3e592b42019-01-11 20:31:29 +01009One of CUE's most powerful features is templates.
10A template defines a value to be unified which each field of a struct.
11
12The template's identifier (in angular brackets) is bound to name of each
13of its sibling fields and is visible within the template value
14that 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.
19job <Name>: {
20 name: Name
Marcel van Lohuizenc9b3cb22019-01-30 11:32:41 +010021 replicas: uint | *1
Marcel van Lohuizen3e592b42019-01-11 20:31:29 +010022 command: string
23}
24
25job list command: "ls"
26
27job nginx: {
28 command: "nginx"
29 replicas: 2
30}
31```
32
33```
34job: {
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 Amsterdame4790382019-01-20 10:29:29 -050046```