String Functions Guide

Updated Jun 19, 2026
DataMagik Documents

String Functions Guide

Format and present text in DataMagik document templates.

Important: The case-conversion helpers upper, lower, title, and trim (and now) are registered for ZPL label templates only — they are not available in HTML/PDF document templates. In document templates, format text with printf and the helpers below.

1. printf — the primary string formatter

Use printf for nearly all text formatting in document templates. It follows Go's formatting verbs.

{{printf "%s" .Name}}            <!-- string -->
{{printf "%.2f" .Amount}}         <!-- 1500.5  -> 1500.50 -->
{{printf "$%.2f" .Total}}         <!-- 1500.50 -> $1500.50 -->
{{printf "%05d" .Number}}         <!-- 42      -> 00042 -->
{{printf "%-20s|" .Label}}        <!-- left-padded to 20 -->
{{printf "Hello, %s!" .Name}}     <!-- combined text -->
VerbMeaningExample
%sString{{printf "%s" .Name}}
%dInteger{{printf "%d" .Qty}}
%.2fFloat, 2 decimals{{printf "%.2f" .Price}}
%05dZero-padded integer{{printf "%05d" .Id}}
%-20sLeft-justified, width 20{{printf "%-20s" .Name}}

2. toString — coerce a value to text

Convert any value (number, boolean, etc.) to its string form before output or comparison.

{{toString .InvoiceNumber}}

3. Safe field access

These document helpers guard against missing fields when working with flexible data:

  • safeGet — return a field's value or an empty string if absent: {{safeGet . "customer_name"}}
  • hasField — test whether a field exists: {{if hasField . "notes"}}...{{end}}
  • isSet / notEmpty — test for present / non-empty values.

4. Case conversion (ZPL label templates only)

Within ZPL label templates you can normalize case and trim whitespace with upper, lower, title, and trim. These are not available in HTML/PDF document templates.

^FD{{upper .PartName}}^FS      <!-- ZPL: WIDGET -->
^FD{{trim .Operator}}^FS       <!-- ZPL: trimmed -->

Quick reference

FunctionAvailable inPurpose
printfDocuments + ZPLFormatted strings/numbers
toStringDocuments + ZPLCoerce value to text
safeGet, hasField, isSet, notEmptyDocumentsSafe field access
upper, lower, title, trimZPL labels onlyCase conversion / trim
Was this page helpful?