Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)

padLeft() : Pad String on Left

Pad a string on the left side to reach a specified length.


On this page

Signature

padLeft(str, totalLength, padChar?)

Parameters

Parameter Type Required Description
str String Yes The string to pad
totalLength Number Yes The desired total length
padChar String No The padding character (default: space)

Returns

Type: String

The padded string.


Examples

Zero-Padded Numbers

<p>Order #{{padLeft(format(model.orderNumber, '0'), 8, '0')}}</p>

Data:

doc.Params["model"] = new {
    orderNumber = 12345
};

Output:

<p>Order #00012345</p>

Invoice Numbers

{{#each model.invoices}}
  <div>INV-{{padLeft(format(this.id, '0'), 6, '0')}}</div>
{{/each}}

Data:

doc.Params["model"] = new {
    invoices = new[] {
        new { id = 1 },
        new { id = 42 },
        new { id = 999 }
    }
};

Output:

<div>INV-000001</div>
<div>INV-000042</div>
<div>INV-000999</div>

Align Text

<pre>
{{#each model.items}}
{{padLeft(this.name, 20, ' ')}} | ${{format(this.price, '0.00')}}
{{/each}}
</pre>

Data:

doc.Params["model"] = new {
    items = new[] {
        new { name = "Widget", price = 19.99m },
        new { name = "Gadget", price = 29.99m },
        new { name = "Tool", price = 9.99m }
    }
};

Output:

<pre>
              Widget | $19.99
              Gadget | $29.99
                Tool | $9.99
</pre>

Custom Padding

<p>{{padLeft(model.code, 10, '-')}}</p>

Data:

doc.Params["model"] = new {
    code = "ABC"
};

Output:

<p>-------ABC</p>

Notes

  • Pads on the left (adds characters before the string)
  • If string is already longer than totalLength, returns unchanged
  • Default padding character is space
  • Pad character must be a single character
  • Useful for formatting numbers and aligning text
  • For right padding, use padRight()

See Also