Jsonnet Engine
Overview
Section titled “Overview”Boilerplate supports Jsonnet as an alternative template engine for generating JSON files. Jsonnet provides functions, imports, and a more structured approach to JSON generation compared to Go templates.
Enabling Jsonnet
Section titled “Enabling Jsonnet”Add an engines section to your boilerplate.yml:
engines: - path: "**/*.jsonnet" template_engine: jsonnetHow It Works
Section titled “How It Works”- Files matching the engine’s
pathglob are processed with the Jsonnet engine instead of Go templates - Boilerplate variables are passed as Jsonnet Top Level Arguments (TLAs)
- The
.jsonnetextension is automatically stripped from output filenames
So config.json.jsonnet becomes config.json in the output.
Example
Section titled “Example”boilerplate.yml
Section titled “boilerplate.yml”variables: - name: ServiceName type: string - name: Port type: int default: 8080 - name: Replicas type: int default: 3
engines: - path: "**/*.jsonnet" template_engine: jsonnetk8s-deployment.json.jsonnet
Section titled “k8s-deployment.json.jsonnet”function(ServiceName, Port, Replicas){ apiVersion: 'apps/v1', kind: 'Deployment', metadata: { name: ServiceName, }, spec: { replicas: std.parseInt(Replicas), template: { spec: { containers: [{ name: ServiceName, port: std.parseInt(Port), }], }, }, },}Variable Access
Section titled “Variable Access”In Jsonnet templates, variables are passed as string TLAs. Use std.parseInt() or std.parseJson() to convert
types as needed.
Mixing Engines
Section titled “Mixing Engines”You can use both Go templates and Jsonnet in the same template directory. Files matching the Jsonnet engine glob use Jsonnet; all other files use Go templates.
engines: - path: "**/*.jsonnet" template_engine: jsonnet
# Regular .go, .md, .yaml files still use Go templates