Templating is a highly effective way to reduce boilerplate by specifying optional fields in bulk. It allows the specification of constraints, or templates, which are unified with every field of a struct that matches a given pattern.

A template’s pattern is an expression in square brackets that selects which fields will be constrained. Each combination of the pattern and a constraint that it applies is called a pattern constraint. Currently, patterns must either be instances of string, or must be _.

Including an alias in the pattern’s square brackets makes the label of each matched field available inside the template.

file.cue
job: {
	nginx: replicas:  2
	manager: command: "monit -I"
	policyd: _
	boltdb: replicas:   3
	postgres: replicas: 5
}

job: [Name=_]: {
	name:     Name // Name is an alias.
	command:  string | *"exec \(Name)"
	replicas: uint | *1
}

// Databases are important, so increase the
// replica minimum.
job: [#DB]: replicas: >=3
#DB: "postgres" | "mysql" | =~"db$"
TERMINAL
$ cue export file.cue
{
    "job": {
        "nginx": {
            "name": "nginx",
            "command": "exec nginx",
            "replicas": 2
        },
        "manager": {
            "name": "manager",
            "command": "monit -I",
            "replicas": 1
        },
        "policyd": {
            "name": "policyd",
            "command": "exec policyd",
            "replicas": 1
        },
        "boltdb": {
            "name": "boltdb",
            "command": "exec boltdb",
            "replicas": 3
        },
        "postgres": {
            "name": "postgres",
            "command": "exec postgres",
            "replicas": 5
        }
    }
}