String Functions Guide
Updated Mar 28, 2026
DataMagik DocumentsString Functions Guide
String manipulation functions for formatting text in your DataMagik document templates.
Table of Contents
- Case Conversion
- String Formatting with printf
- Combining Functions
- Real-World Examples
- Best Practices
1. Case Conversion
upper - Convert to Uppercase
{{.Text | upper}}
<h1>{{.CompanyName | upper}}</h1> <!-- ACME CORPORATION -->
<p>{{.Status | upper}}</p> <!-- ACTIVE -->
<div>{{.ProductCode | upper}}</div> <!-- ABC-123 -->lower - Convert to Lowercase
{{.Text | lower}}
<input type="email" value="{{.Email | lower}}"> <!-- john.doe@example.com -->
<p class="{{.Status | lower}}">{{.Status}}</p> <!-- class="pending" -->title - Convert to Title Case
{{.Text | title}}
<h2>{{.FullName | title}}</h2> <!-- John Doe -->
<p>{{.JobTitle | title}}</p> <!-- Senior Software Engineer -->2. String Formatting with printf
The printf function provides C-style string formatting.
Number Formatting
<p>Price: ${{.Price | printf "%.2f"}}</p> <!-- $1234.50 -->
<p>Percentage: {{.Rate | printf "%.1f"}}%</p> <!-- 15.8% -->
<p>Precise: {{.Value | printf "%.4f"}}</p> <!-- 3.1416 -->Integer Formatting
<p>Quantity: {{.Quantity | printf "%d"}}</p> <!-- 42 -->
<p>Padded: {{.OrderNumber | printf "%05d"}}</p> <!-- 00123 -->
<p>Hex: {{.ColorCode | printf "%x"}}</p> <!-- ff -->String Formatting
<p>{{.Name | printf "Welcome, %s!"}}</p> <!-- Welcome, Alice! -->
<p>{{.Code | printf "Code: %10s"}}</p> <!-- Right-aligned -->Common printf Format Specifiers
| Specifier | Description | Example | Output |
|---|---|---|---|
%s | String | "Hello" | printf "%s" | Hello |
%d | Integer | 42 | printf "%d" | 42 |
%f | Float | 3.14 | printf "%f" | 3.140000 |
%.2f | Float (2 decimals) | 3.14159 | printf "%.2f" | 3.14 |
%05d | Zero-padded integer | 123 | printf "%05d" | 00123 |
%x | Hexadecimal | 255 | printf "%x" | ff |
%X | Hexadecimal (uppercase) | 255 | printf "%X" | FF |
%10s | Right-aligned (width 10) | "Hi" | printf "%10s" | Hi |
%-10s | Left-aligned (width 10) | "Hi" | printf "%-10s" | Hi |
Currency Formatting
<table>
<tr><td>Subtotal:</td><td>${{.Subtotal | printf "%.2f"}}</td></tr>
<tr><td>Tax:</td><td>${{.Tax | printf "%.2f"}}</td></tr>
<tr><td><strong>Total:</strong></td><td><strong>${{.Total | printf "%.2f"}}</strong></td></tr>
</table>3. Combining Functions
Chain multiple string functions using pipes:
<!-- Convert to lowercase, then title case -->
<p>{{.Name | lower | title}}</p>
<!-- Convert to upper and format -->
<p>{{.Code | upper | printf "Product Code: %s"}}</p>
<!-- Email in lowercase with label -->
<p>{{.Email | lower | printf "Contact: %s"}}</p>
<!-- Status badge with uppercase -->
<span class="badge {{.Status | lower}}">
{{.Status | upper}}
</span>4. Real-World Examples
Business Card
<div class="business-card">
<h2>{{.Name | title}}</h2>
<p class="title">{{.JobTitle | title}}</p>
<p>{{.Department | upper}}</p>
<p>{{.Email | lower}}</p>
<p>{{.Phone | printf "Tel: %s"}}</p>
</div>Product Label
<div class="product-label">
<h1>{{.ProductName | upper}}</h1>
<p>SKU: {{.SKU | printf "%08d"}}</p>
<p>Category: {{.Category | title}}</p>
<p class="price">${{.Price | printf "%.2f"}}</p>
{{if .OnSale}}
<p class="sale">Was: ${{.OriginalPrice | printf "%.2f"}}</p>
<p class="savings">Save: ${{.Savings | printf "%.2f"}}</p>
{{end}}
</div>Invoice Line Items
<table>
<thead>
<tr><th>Description</th><th>Qty</th><th>Unit Price</th><th>Total</th></tr>
</thead>
<tbody>
{{range .Items}}
<tr>
<td>{{.Description | title}}</td>
<td>{{.Quantity | printf "%d"}}</td>
<td>${{.UnitPrice | printf "%.2f"}}</td>
<td>${{.Total | printf "%.2f"}}</td>
</tr>
{{end}}
</tbody>
</table>Status Badges
{{range .Orders}}
<div class="order">
<span class="order-id">Order: {{.ID | printf "#%05d"}}</span>
<span class="badge badge-{{.Status | lower}}">{{.Status | upper}}</span>
</div>
{{end}}5. Best Practices
- Email Addresses — Always use
lower - Currency — Always use
printf "%.2f"for monetary values - CSS Classes — Use
lowerfor dynamic CSS class names - IDs and Codes — Use
upperfor product codes and reference IDs - Names — Use
titlefor person names and titles - Consistency — Apply the same formatting rules throughout your template
Function Quick Reference
| Function | Description | Example |
|---|---|---|
upper | ALL CAPS | {{.Name | upper}} |
lower | all lowercase | {{.Email | lower}} |
title | Title Case | {{.Name | title}} |
printf | Formatted output | {{.Price | printf "%.2f"}} |