blob: 7220f1743774a97854b7a9d476204f11974a69f0 [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.
Dr. Stefan Schimanski8e430452019-05-16 21:16:21 +000010A template defines a value to be unified with each field of a struct.
Marcel van Lohuizen3e592b42019-01-11 20:31:29 +010011
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
Marcel van Lohuizenf0c94042019-02-22 22:46:37 +010016<!-- CUE editor -->
17_templates.cue:_
Marcel van Lohuizen3e592b42019-01-11 20:31:29 +010018```
19// The following struct is unified with all elements in job.
20// The name of each element is bound to Name and visible in the struct.
21job <Name>: {
22 name: Name
Marcel van Lohuizenc9b3cb22019-01-30 11:32:41 +010023 replicas: uint | *1
Marcel van Lohuizen3e592b42019-01-11 20:31:29 +010024 command: string
25}
26
27job list command: "ls"
28
29job nginx: {
30 command: "nginx"
31 replicas: 2
32}
33```
34
Marcel van Lohuizenf0c94042019-02-22 22:46:37 +010035<!-- JSON result -->
36`$ cue eval templates.cue`
Marcel van Lohuizen3e592b42019-01-11 20:31:29 +010037```
38job: {
39 list: {
40 name: "list"
41 replicas: 1
42 command: "ls"
43 }
44 nginx: {
45 name: "nginx"
46 replicas: 2
47 command: "nginx"
48 }
49}
Jonathan Amsterdame4790382019-01-20 10:29:29 -050050```