# Go Template Helpers Custom `html/template` FuncMap extensions that bring Liquid-like expressiveness to Go server-side templates. --- ## String Helpers ### `capitalize` Uppercases the first letter of a string and lowercases the rest. ```html

{{capitalize .Title}}

``` --- ### `truncate` Cuts a string to at most `n` characters and appends `…` if trimmed. Designed for pipeline use — pass `n` first. ```html

{{.Excerpt | truncate 120}}

``` --- ### `prepend` Adds a prefix to the beginning of a string. Designed for pipeline use — pass the prefix first. ```html Read more ``` --- ### `strAppend` Adds a suffix to the end of a string. Designed for pipeline use — pass the suffix first. ```html Read more ``` --- ### `split` Divides a string into a slice of substrings by a separator. Designed for pipeline use — pass the separator first. Useful combined with `range` or `join`. ```html {{range split "," .TagString}} {{.}} {{end}} ``` --- ### `join` Concatenates a string slice into a single string with a separator. ```html

{{join .Tags " · "}}

``` --- ## Number Helpers ### `fmtNumber` Formats an integer or float with thousands separators using the English locale. ```html {{fmtNumber .Views}} views $ {{fmtNumber .Price}} ``` --- ## Date & Time Helpers ### `fmtDate` Formats a `time.Time` value using a named layout or any custom Go layout string. | Named layout | Output example | |---|---| | `"short"` | `02 Jan 2006` | | `"long"` | `02 January 2006` | | `"iso"` | `2006-01-02` | | `"datetime"` | `02 Jan 2006 15:04` | ```html ``` --- ### `timeAgo` Returns a human-readable relative time string from now (`"just now"`, `"3 hours ago"`, `"2 days ago"`, etc.). ```html Published {{timeAgo .PublishedAt}} ``` --- ## Collection Helpers ### `first` Returns the first element of a slice, or `nil` if the slice is empty. ```html {{with first .Articles}}

{{capitalize .Title}}

{{end}} ``` --- ### `last` Returns the last element of a slice, or `nil` if the slice is empty. ```html {{with last .Articles}}

Latest: {{capitalize .Title}}

{{end}} ``` --- ### `sliceOf` Returns a sub-range of a slice from index `start` (inclusive) to `end` (exclusive). ```html {{range sliceOf .Articles 0 3}}
  • {{capitalize .Title}}
  • {{end}} ``` --- ### `contains` Reports whether an item is present in a slice, or a substring exists within a string. ```html {{if contains .Tags "go"}} Go {{end}} {{if contains .Bio "engineer"}}

    Engineering post

    {{end}} ``` --- ## Logic Helpers ### `defaultVal` Returns a fallback value if the given value is nil or its zero value. ```html

    {{defaultVal "No subtitle" .Subtitle}}

    ``` --- ### `ternary` Returns `trueVal` if the condition is true, `falseVal` otherwise. Pass the true value first, then the false value, then the condition. ```html {{ternary "Active" "Inactive" .IsActive}} ``` --- ### `coalesce` Returns the first non-empty, non-nil value from a list of arguments. ```html

    {{coalesce .Nickname .FullName "Anonymous"}}

    ``` --- ## Struct Helpers ### `hasField` Reports whether a struct has a field with the given name. Useful for rendering shared templates across different data types. ```html {{if hasField . "Subtitle"}}

    {{.Subtitle}}

    {{end}} ``` --- ## Pipeline Chaining Helpers designed for pipeline use (`truncate`, `prepend`, `strAppend`, `split`) accept their configuration argument first and the input string last, so they compose naturally with `|`. ```html {{/* chain multiple helpers */}} {{.Title | capitalize | truncate 40}} {{/* build a URL from a slug */}} {{.Title | capitalize}} ```