Go Template Syntax Guide
Updated Mar 28, 2026
DataMagik DocumentsGo Template Syntax Guide
Fundamentals of Go template syntax for dynamic content generation in DataMagik Document Designer.
Table of Contents
- Basic Variable Substitution
- Nested Object Access
- Array Access
- Comments
- Whitespace Control
- Pipeline Operations
- Variables in Templates
- Complete Example
1. Basic Variable Substitution
Access data fields using dot notation with double curly braces:
<h1>{{.Title}}</h1>
<p>{{.Description}}</p>
<span>{{.Amount}}</span>Multiple variables in one line:
<p>Hello {{.FirstName}} {{.LastName}}, your balance is ${{.Balance}}</p>2. Nested Object Access
Use dot notation to access nested object properties:
<h2>{{.Customer.Name}}</h2>
<p>Email: {{.Customer.Email}}</p>
<p>{{.Customer.Address.Street}}</p>
<p>{{.Customer.Address.City}}, {{.Customer.Address.State}} {{.Customer.Address.Zip}}</p>Deep nesting:
<p>Shipped to: {{.Order.Shipping.Recipient.Name}}</p>
<p>Tracking: {{.Order.Shipping.TrackingNumber}}</p>3. Array Access
Direct Index Access
<p>First item: {{index .Items 0}}</p>
<p>Second item: {{index .Items 1}}</p>Accessing Array of Objects
<p>First customer: {{(index .Customers 0).Name}}</p>
<p>First customer email: {{(index .Customers 0).Email}}</p>4. Comments
Comments are not rendered in the output:
{{/* This is a comment and won't appear in output */}}
<p>{{.Message}}</p>
{{/* Multi-line comment
It can span multiple lines
and won't appear in the output */}}
{{/* Customer Information Section */}}
<div class="customer-info">
{{/* Display customer name */}}
<h2>{{.Customer.Name}}</h2>
</div>5. Whitespace Control
Control whitespace around template tags using - trim markers:
<!-- Default: whitespace preserved -->
<p> {{.Name}}</p>
<!-- Trim before: {{- removes leading whitespace -->
<p> {{- .Name}}</p>
<!-- Trim after: -}} removes trailing whitespace -->
<p> {{.Name -}}</p>
<!-- Trim both sides -->
<ul>
{{- range .Items}}
<li>{{- .Name -}}</li>
{{- end}}
</ul>6. Pipeline Operations
Chain operations using the pipe operator |:
<!-- Case conversion -->
<p>{{.Name | upper}}</p>
<p>{{.Description | lower}}</p>
<p>{{.Title | title}}</p>
<!-- Formatted output -->
<p>{{.Price | printf "%.2f"}}</p>
<p>{{.Date | dateFormat "January 2, 2006"}}</p>
<!-- Multiple pipes -->
<p>{{.Email | lower | printf "Contact: %s"}}</p>7. Variables in Templates
Declaring Variables
{{$total := .Amount}}
<p>Amount: ${{$total}}</p>Variables in Loops
{{range $index, $item := .Products}}
<div>
<p>{{$index}}. {{$item.Name}}</p>
<p>Price: ${{$item.Price}}</p>
</div>
{{end}}Capturing Root Context
Save the current context to access it inside range or with blocks:
{{$root := .}}
{{range .Orders}}
<div>
<p>Order: {{.Number}}</p>
<p>Company: {{$root.CompanyName}}</p>
</div>
{{end}}8. Complete Example: Invoice Template
<div class="header">
<h1>{{.Company.Name | upper}}</h1>
<p>{{.Company.Address.Street}}</p>
<p>{{.Company.Address.City}}, {{.Company.Address.State}} {{.Company.Address.Zip}}</p>
</div>
<div class="details">
<h2>Invoice #{{.InvoiceNumber}}</h2>
<p>Date: {{.InvoiceDate | dateFormat "January 2, 2006"}}</p>
<p>Due: {{.DueDate | dateFormat "January 2, 2006"}}</p>
</div>
<div class="customer">
<h3>Bill To:</h3>
<p>{{.Customer.Name}}</p>
<p>{{.Customer.Email}}</p>
</div>
<table>
<thead>
<tr><th>Description</th><th>Qty</th><th>Price</th><th>Total</th></tr>
</thead>
<tbody>
{{- range .Items}}
<tr>
<td>{{.Description}}</td>
<td>{{.Quantity}}</td>
<td>${{.Price | printf "%.2f"}}</td>
<td>${{.Total | printf "%.2f"}}</td>
</tr>
{{- end}}
</tbody>
</table>
<div class="total">
<p>Subtotal: ${{.Subtotal | printf "%.2f"}}</p>
<p>Tax ({{.TaxRate}}%): ${{.Tax | printf "%.2f"}}</p>
<p>Total: ${{.TotalAmount | printf "%.2f"}}</p>
</div>For more on string manipulation, see the String Functions Guide. For conditional logic, see the Control Flow Guide. For date formatting, see the Date & Time Functions Guide.