String Functions Guide

Last updated: October 6, 2025

String Functions Guide

DataMagik Document Designer provides powerful string manipulation functions for formatting text in your templates.

Table of Contents

Case Conversion

upper - Convert to Uppercase

Converts all characters to uppercase.

Syntax:

{{.Text | upper}}

Examples:

<h1>{{.CompanyName | upper}}</h1>
<p>{{.Status | upper}}</p>
<div>{{.ProductCode | upper}}</div>

Sample Data:

{
  "CompanyName": "Acme Corporation",
  "Status": "active",
  "ProductCode": "abc-123"
}

Output:

<h1>ACME CORPORATION</h1>
<p>ACTIVE</p>
<div>ABC-123</div>

Use Cases:

lower - Convert to Lowercase

Converts all characters to lowercase.

Syntax:

{{.Text | lower}}

Examples:

<input type="email" value="{{.Email | lower}}">
<p class="{{.Status | lower}}">Status: {{.Status}}</p>
<a href="mailto:{{.ContactEmail | lower}}">Contact</a>

Sample Data:

{
  "Email": "John.Doe@Example.COM",
  "Status": "PENDING",
  "ContactEmail": "SALES@COMPANY.COM"
}

Output:

<input type="email" value="john.doe@example.com">
<p class="pending">Status: PENDING</p>
<a href="mailto:sales@company.com">Contact</a>

Use Cases:

title - Convert to Title Case

Converts the first letter of each word to uppercase.

Syntax:

{{.Text | title}}

Examples:

<h2>{{.FullName | title}}</h2>
<p>{{.JobTitle | title}}</p>
<span>{{.Department | title}}</span>

Sample Data:

{
  "FullName": "john doe",
  "JobTitle": "senior software engineer",
  "Department": "information technology"
}

Output:

<h2>John Doe</h2>
<p>Senior Software Engineer</p>
<span>Information Technology</span>

Use Cases:

String Formatting with printf

The <code>printf</code> function provides C-style string formatting for precise control over output.

Basic Number Formatting

Format Decimal Places:

<p>Price: ${{.Price | printf "%.2f"}}</p>
<p>Percentage: {{.Rate | printf "%.1f"}}%</p>
<p>Precise: {{.Value | printf "%.4f"}}</p>

Sample Data:

{
  "Price": 1234.5,
  "Rate": 15.75,
  "Value": 3.141592653589793
}

Output:

<p>Price: $1234.50</p>
<p>Percentage: 15.8%</p>
<p>Precise: 3.1416</p>

Integer Formatting

<p>Quantity: {{.Quantity | printf "%d"}}</p>
<p>Padded: {{.OrderNumber | printf "%05d"}}</p>
<p>Hex: {{.ColorCode | printf "%x"}}</p>

Sample Data:

{
  "Quantity": 42,
  "OrderNumber": 123,
  "ColorCode": 255
}

Output:

<p>Quantity: 42</p>
<p>Padded: 00123</p>
<p>Hex: ff</p>

String Formatting

<p>{{.Name | printf "Welcome, %s!"}}</p>
<p>{{.Status | printf "Status: %s"}}</p>
<p>{{.Code | printf "Code: %10s"}}</p> <!-- Right-aligned, width 10 -->

Sample Data:

{
  "Name": "Alice",
  "Status": "Active",
  "Code": "ABC"
}

Output:

<p>Welcome, Alice!</p>
<p>Status: Active</p>
<p>Code:        ABC</p>

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 Examples

<table>
  <tr>
    <td>Subtotal:</td>
    <td>${{.Subtotal | printf "%.2f"}}</td>
  </tr>
  <tr>
    <td>Tax:</td>
    <td>${{.Tax | printf "%.2f"}}</td>
  </tr>
  <tr>
    <td>Shipping:</td>
    <td>${{.Shipping | printf "%.2f"}}</td>
  </tr>
  <tr>
    <td><strong>Total:</strong></td>
    <td><strong>${{.Total | printf "%.2f"}}</strong></td>
  </tr>
</table>

Sample Data:

{
  "Subtotal": 99.95,
  "Tax": 8.50,
  "Shipping": 5.99,
  "Total": 114.44
}

Combining Functions

Chaining Multiple Operations

Combine 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>

<!-- Format number then add text -->
<p>{{.Amount | printf "%.2f" | printf "Total: $%s"}}</p>

Sample Data:

{
  "Name": "JOHN DOE",
  "Code": "prod-abc-123",
  "Amount": 1234.5
}

Output:

<p>John Doe</p>
<p>Product Code: PROD-ABC-123</p>
<p>Total: $1234.50</p>

Complex Formatting Chains

<!-- Email in lowercase with label -->
<p>{{.Email | lower | printf "Contact: %s"}}</p>

<!-- Status badge with uppercase -->
<span class="badge {{.Status | lower}}">
  {{.Status | upper}}
</span>

<!-- Formatted currency with thousands separator -->
<p>Revenue: ${{.Revenue | printf "%.2f"}}</p>

Real-World Examples

Example 1: 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>

Sample Data:

{
  "Name": "jane smith",
  "JobTitle": "marketing director",
  "Department": "marketing",
  "Email": "Jane.Smith@Company.COM",
  "Phone": "+1-555-123-4567"
}

Output:

<div class="business-card">
  <h2>Jane Smith</h2>
  <p class="title">Marketing Director</p>
  <p>MARKETING</p>
  <p>jane.smith@company.com</p>
  <p>Tel: +1-555-123-4567</p>
</div>

Example 2: 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>

Sample Data:

{
  "ProductName": "premium wireless headphones",
  "SKU": 12345,
  "Category": "electronics",
  "Price": 149.99,
  "OnSale": true,
  "OriginalPrice": 199.99,
  "Savings": 50.00
}

Output:

<div class="product-label">
  <h1>PREMIUM WIRELESS HEADPHONES</h1>
  <p>SKU: 00012345</p>
  <p>Category: Electronics</p>
  <p class="price">$149.99</p>
  <p class="sale">Was: $199.99</p>
  <p class="savings">Save: $50.00</p>
</div>

Example 3: Invoice Line Items

<table class="invoice-items">
  <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>

Sample Data:

{
  "Items": [
    {
      "Description": "consulting services",
      "Quantity": 10,
      "UnitPrice": 150.0,
      "Total": 1500.0
    },
    {
      "Description": "software license",
      "Quantity": 1,
      "UnitPrice": 499.99,
      "Total": 499.99
    }
  ]
}

Output:

<table class="invoice-items">
  <thead>
    <tr>
      <th>Description</th>
      <th>Qty</th>
      <th>Unit Price</th>
      <th>Total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Consulting Services</td>
      <td>10</td>
      <td>$150.00</td>
      <td>$1500.00</td>
    </tr>
    <tr>
      <td>Software License</td>
      <td>1</td>
      <td>$499.99</td>
      <td>$499.99</td>
    </tr>
  </tbody>
</table>

Example 4: Status Badges

<div class="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}}
</div>

Sample Data:

{
  "Orders": [
    {"ID": 1, "Status": "pending"},
    {"ID": 42, "Status": "shipped"},
    {"ID": 123, "Status": "delivered"}
  ]
}

Output:

<div class="status-badges">
  <div class="order">
    <span class="order-id">Order: #00001</span>
    <span class="badge badge-pending">PENDING</span>
  </div>
  <div class="order">
    <span class="order-id">Order: #00042</span>
    <span class="badge badge-shipped">SHIPPED</span>
  </div>
  <div class="order">
    <span class="order-id">Order: #00123</span>
    <span class="badge badge-delivered">DELIVERED</span>
  </div>
</div>

Best Practices

  1. Email Addresses: Always use lower for email addresses
  2. Currency: Always use printf "%.2f" for monetary values
  3. CSS Classes: Use lower for dynamic CSS class names
  4. IDs and Codes: Use upper for product codes and reference IDs
  5. Names: Use title for person names and titles
  6. Consistency: Apply the same formatting rules throughout your template

Next Steps


Function Reference Quick Guide:

  • upper - ALL CAPS
  • lower - all lowercase
  • title - Title Case
  • printf - Formatted output
Was this page helpful?