Built-in template functions
This page lists all built-in template functions available in go-template-docx. These functions allow you to style text, insert images, handle charts, and more.
Text Styling Functions
Multi-Style Functions
inlineStyledText(text string, styles ...interface{})
Apply multiple styles at once using variadic parameters.
{{inlineStyledText .Text "b" "i" "fs:14" "bg:#C0FFEE" "#FF0000"}}This example applies: bold, italic, 14pt font, light blue background, and red text color.
styledText(text string, styles []interface{})
Apply multiple styles using a slice. Useful when styles are computed in Go.
{{styledText .Text (list "b" "i" "fs:14" "bg:#C0FFEE" "#FF0000")}}
{{styledText .Text .MyStylesSlice}}Available Style Codes:
| Style | Codes | Description |
|---|---|---|
| Bold | b, bold | Makes text bold |
| Italic | i, italic | Makes text italic |
| Underline | u, underline | Underlines text |
| Strikethrough | s, strike, strikethrough | Strikes through text |
| Font Size | fontSize:<size>, fs:<size> | Sets font size in points (e.g., fs:14) |
| Text Color | #RRGGBB, RRGGBB | Sets text color (e.g., #FF0000 for red) |
| Highlight | <color> | Applies highlight color (see OOXML colors) |
Single-Style Functions
bold(s string)
{{bold .Text}}italic(s string)
{{italic .Text}}underline(s string)
{{underline .Text}}strike(s string)
{{strike .Text}}fontSize(s string, size int)
{{fontSize .Text 14}}color(s string, hex string)
Apply text color. Hex format: RRGGBB or #RRGGBB.
{{color .Text "FF0000"}}
{{color .Text "#FF0000"}}highlight(s string, color string)
Apply highlight color. See OOXML colors for available colors.
{{highlight .Text "yellow"}}shadeTextBg(s string, hex string)
Apply background shading to text. Hex format: RRGGBB or #RRGGBB.
{{shadeTextBg .Text "C0FFEE"}}
{{shadeTextBg .Text .ColorHex}}Image Functions
image(filename string)
Insert an image. Maintains original aspect ratio. The filename must match a loaded media file.
{{image "logo.png"}}
{{image .ImageFilename}}replaceImage(filename string)
Replace an existing image while preserving size and position. Use this in the image's alt-text field.
{{replaceImage "photo.jpg"}}
{{replaceImage .ImageFilename}}Text Formatting Functions
preserveNewline(text string)
Treat newlines as soft breaks (SHIFT + ENTER), keeping text in the same paragraph.
{{preserveNewline .TextWithNewlines}}breakParagraph(text string)
Treat newlines as hard breaks (ENTER), creating new paragraphs.
{{breakParagraph .TextWithNewlines}}Chart Functions
toNumberCell(v any)
For Excel cells in charts: sets cell type to number. Required for charts to work correctly.
{{toNumberCell .Revenue}}
{{toNumberCell 42.5}}The value can be any type convertible to float64.
Shape Functions
shapeBgFillColor(hex string)
Change a shape's background fill color. Use this in the shape's alt-text field.
{{shapeBgFillColor "FF0000"}}
{{shapeBgFillColor .ShapeBgHex}}Hex format: RRGGBB or #RRGGBB.
Table Functions
tableCellBgColor(hex string)
Change table cell background color. Use inside the cell content.
{{tableCellBgColor "00FF00"}}Active
{{tableCellBgColor .CellBgHex}}{{.Status}}Hex format: RRGGBB or #RRGGBB.
Utility Functions
list(v ...interface{}) []interface{}
Create a slice from variadic parameters. Useful for styledText or template functions that accepts lists.
{{list "b" "i" "fs:14"}}