Template Syntax Reference
Complete reference for template variables, custom functions, the Sprig library, and advanced techniques.
Related pages
- Templates Overview — Quick start and best practices
- Type Conversion —
int,float,boolfor Kubernetes fields - Debugging & Migration — Errors and template evolution
Available Variables
Required Variables
These are always available from the template context:
.uid # Node unique identifier (from uid mapping)
.activate # Activation status (from activate mapping)Context Variables
Automatically provided:
.hubId # LynqHub name
.templateRef # LynqForm nameCustom Variables
From extraValueMappings in LynqHub:
spec:
extraValueMappings:
planId: subscription_plan
region: deployment_region
dbHost: database_hostAccess in templates:
.planId # Maps to subscription_plan column
.region # Maps to deployment_region column
.dbHost # Maps to database_host columnDeprecated Variables
DEPRECATED
The following variables are deprecated since v1.1.11 and will be removed in v1.3.0:
.hostOrUrl # Original URL/host from hub (from hostOrUrl mapping)
.host # Auto-extracted host from .hostOrUrlMigration: Use extraValueMappings + the toHost() function instead. See Debugging & Migration.
Built-in Custom Functions
toHost(url)
Extract hostname from URL:
# Input: https://acme.example.com:8080/path
# Output: acme.example.com
env:
- name: HOST
value: "{{ .domainUrl | toHost }}" # Use with extraValueMappingstrunc63(s)
Truncate to 63 characters (Kubernetes name limit):
nameTemplate: "{{ printf \"%s-%s-deployment\" .uid .region | trunc63 }}"sha1sum(s)
Generate SHA1 hash — useful for short, stable, unique resource names:
nameTemplate: "app-{{ .uid | sha1sum | trunc 8 }}"fromJson(s)
Parse JSON string from a database column:
# Database column: config = '{"apiKey":"sk-abc","endpoint":"https://api.example.com"}'
env:
- name: API_KEY
value: "{{ (.config | fromJson).apiKey }}"
- name: ENDPOINT
value: "{{ (.config | fromJson).endpoint }}"Sprig Functions (200+)
Full documentation: https://masterminds.github.io/sprig/
String Functions
nameTemplate: "{{ .uid | upper }}"
nameTemplate: "{{ .uid | lower }}"
value: "{{ .name | trim }}"
value: "{{ .uid | replace \".\" \"-\" }}"
value: "{{ .name | quote }}"Encoding Functions
value: "{{ .secret | b64enc }}"
value: "{{ .encoded | b64dec }}"
value: "{{ .param | urlquery }}"
value: "{{ .data | sha256sum }}"Default Values
image: "{{ .deployImage | default \"nginx:stable\" }}"
port: "{{ .appPort | default \"8080\" }}"
region: "{{ .region | default \"us-east-1\" }}"Conditionals
Prefer ternary for binary switches — it stays on one line and reads left-to-right:
# ternary vTrue vFalse condition
env:
- name: DEBUG
value: "{{ ternary \"true\" \"false\" (eq .planId \"enterprise\") }}"
replicas: {{ ternary 5 2 (eq .planId "enterprise") | int }}Use if/else when constructing strings from multiple variables or when the condition is compound:
- name: HOST
value: "{{ if and .customDomain (eq .domainVerified \"true\") }}{{ .customDomain }}{{ else }}{{ .uid }}.default.svc{{ end }}"Lists and Iteration
annotations:
tags: "{{ list \"app\" .uid .region | join \",\" }}"Math Functions
value: "{{ add .basePort 1000 }}"
value: "{{ mul .cpuLimit 2 }}"
value: "{{ max .minReplicas 3 }}"Template Rendering Process
1. Variable Collection
The hub controller collects variables from the database row and merges them with context:
uid = "acme-corp"
activate = true
planId = "enterprise" # from extraValueMappings
domainUrl = "https://acme.ex.com" # from extraValueMappings
hubId = "customer-hub" # context
templateRef = "customer-web-app" # context2. Template Evaluation
For each resource in the LynqForm:
- Render
nameTemplate→ resource name - Render
labelsTemplate→ labels map - Render
annotationsTemplate→ annotations map - Render
spec→ recursively render all string values
3. Resource Application
The rendered resource is applied to Kubernetes using Server-Side Apply with fieldManager: lynq.
Advanced Template Techniques
Local Variables
Define a local variable to avoid repeating complex expressions:
nameTemplate: |
{{- $base := printf "%s-%s" .uid (.region | default "default") -}}
{{ $base | trunc63 }}-webMulti-Tier with Shared Variable
deployments:
- id: web
nameTemplate: |
{{- $base := printf "%s-%s" .uid (.region | default "default") -}}
{{ $base | trunc63 }}-web
spec:
apiVersion: apps/v1
kind: Deployment
spec:
replicas: "{{ .webReplicas | default \"2\" | int }}"
template:
spec:
containers:
- name: web
image: "{{ .webImage | default \"nginx:stable\" }}"
env:
{{- $apiHost := printf "%s-api-svc.%s.svc.cluster.local" .uid .namespace }}
- name: API_ENDPOINT
value: "http://{{ $apiHost }}"
- id: api
nameTemplate: |
{{- $base := printf "%s-%s" .uid (.region | default "default") -}}
{{ $base | trunc63 }}-api
dependIds: [web]
spec:
apiVersion: apps/v1
kind: Deployment
spec:
replicas: "{{ .apiReplicas | default \"3\" | int }}"
template:
spec:
containers:
- name: api
image: "{{ .apiImage | default \"api:latest\" }}"
env:
{{- $dbHost := printf "%s-db.%s.svc.cluster.local" .uid .namespace }}
- name: DATABASE_URL
value: "postgres://{{ $dbHost }}:5432/{{ .uid }}"Range Over Lists
data:
endpoints.txt: |
{{- range $i, $region := list "us-east-1" "us-west-2" "eu-west-1" }}
{{ $region }}.example.com
{{- end }}Complex JSON Parsing
# Database column: config = '{"db":{"host":"localhost","port":5432}}'
env:
- name: DB_HOST
value: "{{ ((.config | fromJson).db).host }}"
- name: DB_PORT
value: "{{ ((.config | fromJson).db).port }}"See Also
- Templates Overview — Quick start examples and best practices
- Type Conversion —
int,float,boolfor numeric/boolean Kubernetes fields - Debugging & Migration — Common errors, debugging tools, and template evolution
- API Reference — LynqForm CRD schema
