String Functions Guide

Updated Mar 28, 2026
DataMagik Documents

String Functions Guide

String manipulation functions for formatting text in your DataMagik document templates.

Table of Contents

  1. Case Conversion
  2. String Formatting with printf
  3. Combining Functions
  4. Real-World Examples
  5. 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

SpecifierDescriptionExampleOutput
%sString"Hello" | printf "%s"Hello
%dInteger42 | printf "%d"42
%fFloat3.14 | printf "%f"3.140000
%.2fFloat (2 decimals)3.14159 | printf "%.2f"3.14
%05dZero-padded integer123 | printf "%05d"00123
%xHexadecimal255 | printf "%x"ff
%XHexadecimal (uppercase)255 | printf "%X"FF
%10sRight-aligned (width 10)"Hi" | printf "%10s"        Hi
%-10sLeft-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 lower for dynamic CSS class names
  • IDs and Codes — Use upper for product codes and reference IDs
  • Names — Use title for person names and titles
  • Consistency — Apply the same formatting rules throughout your template

Function Quick Reference

FunctionDescriptionExample
upperALL CAPS{{.Name | upper}}
lowerall lowercase{{.Email | lower}}
titleTitle Case{{.Name | title}}
printfFormatted output{{.Price | printf "%.2f"}}
Was this page helpful?