Documentation
¶
Overview ¶
Package godtpl renders OpenDocument Text (.odt) files as Go templates.
An ODT file is a ZIP archive whose main content lives in content.xml. This package treats that XML as a Go text/template, renders it with a user-supplied context map, and writes the result back into a new ZIP.
Basic usage ¶
tpl, err := godtpl.New("template.odt")
if err != nil { ... }
err = tpl.Render(map[string]any{
"company": "ACME",
"items": []map[string]any{...},
})
if err != nil { ... }
err = tpl.SaveTo("output.odt")
Template syntax ¶
Templates use Go's text/template syntax. Variables in the context map are accessed with a leading dot:
{{ .company }} simple substitution
{{ range .items }} loop
{{ .name }}
{{ end }}
{{ if .show }} … {{ end }} conditional
To escape a literal {{ write {_{ and }_} for }}:
{_{ and }_} → {{ and }} in the output
Shorthand prefixes ¶
To control an entire ODF element (e.g. a table row) from a template tag, add a shorthand prefix to strip the surrounding element:
{%tr range .items %} or {{tr range .items }} → {{ range .items }} (strips <table:table-row>)
{%tr end %} or {{tr end }} → {{ end }}
{%tc STMT %} or {{tc STMT }} → controls <table:table-cell>
{%p STMT %} or {{p STMT }} → controls <text:p>
{%s STMT %} or {{s STMT }} → controls <text:span>
Special context types ¶
- NewListing – multi-line text with \n, \t, \a, \f support
- NewRichText – inline text with bold, italic, color, etc.
- NewRichTextParagraph – block-level formatted paragraphs
- NewInlineImage – embed an image
- [(*Template).NewSubdoc] – embed another ODT file
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type InlineImage ¶
type InlineImage struct {
Width string // ODF length string, e.g. "5cm", "2in", "96pt"
Height string
Anchor string // text:anchor-type; defaults to "as-char" (inline)
// contains filtered or unexported fields
}
InlineImage embeds a picture inline inside an ODF paragraph.
In ODF, images are stored in the Pictures/ directory inside the ZIP archive and referenced from content.xml via <draw:frame>/<draw:image> elements.
Usage:
img, err := godtpl.NewInlineImage(tpl, "logo.png") img.Width = "4cm" img.Height = "2cm" context["logo"] = img
In the .odt template:
{{ .logo }}
func NewInlineImage ¶
func NewInlineImage(tpl *Template, path string) (*InlineImage, error)
NewInlineImage creates an InlineImage by reading the file at path.
func NewInlineImageFromReader ¶
NewInlineImageFromReader creates an InlineImage from an io.Reader. ext is the file extension without the leading dot (e.g. "png").
func (*InlineImage) String ¶
func (img *InlineImage) String() string
String implements fmt.Stringer. Called by text/template during rendering.
type Listing ¶
type Listing struct {
// contains filtered or unexported fields
}
Listing wraps multi-line / tabulated text for use in an ODF template.
After template rendering, special characters in the text are converted to proper ODF inline elements by the template engine:
- \n → <text:line-break/> (soft line break inside the same paragraph)
- \t → <text:tab/>
- \a → closes and reopens <text:p> (new paragraph, same style)
- \f → soft page break + new <text:p>
Usage in Go code:
context["body"] = godtpl.NewListing("Line one\nLine two")
Usage in the .odt template:
{{ .body }}
func NewListing ¶
NewListing creates a Listing from a plain text string. HTML special characters (<, >, &) are escaped so they are safe in XML. The control characters \n, \t, \a, \f are preserved for later conversion.
type R ¶
type R = RichText
R is a convenience alias for RichText (mirrors the Python docxtpl naming).
type RichText ¶
type RichText struct {
// contains filtered or unexported fields
}
RichText builds an inline rich-text fragment for use inside an existing ODF paragraph. It implements fmt.Stringer so that text/template inserts the generated XML verbatim.
Usage:
rt := godtpl.NewRichText(tpl)
rt.Add("Hello ", TextProps{Bold: true})
rt.Add("world", TextProps{Color: "#FF0000"})
context["greeting"] = rt
In the .odt template:
{{ .greeting }}
func NewRichText ¶
NewRichText creates a new empty RichText associated with the given template.
func (*RichText) Add ¶
Add appends a text run with the given formatting properties. If props is zero-valued, the text is inserted without any style wrapper.
func (*RichText) AddStyled ¶
AddStyled appends text wrapped in a named character style from the ODT template.
type RichTextParagraph ¶
type RichTextParagraph struct {
// contains filtered or unexported fields
}
RichTextParagraph builds one or more complete <text:p> paragraphs for block-level insertion. Use with the {{p ...}} shorthand in the template.
Usage:
rp := godtpl.NewRichTextParagraph(tpl)
rt := godtpl.NewRichText(tpl)
rt.Add("Bold heading", godtpl.TextProps{Bold: true})
rp.Add(rt, "Heading_20_2")
context["header"] = rp
In the .odt template (block-level, strips surrounding <text:p>):
{{p .header }}
func NewRichTextParagraph ¶
func NewRichTextParagraph(tpl *Template) *RichTextParagraph
NewRichTextParagraph creates a new empty RichTextParagraph.
func (*RichTextParagraph) Add ¶
func (rp *RichTextParagraph) Add(content fmt.Stringer, paraStyle string)
Add appends a paragraph. content may be a *RichText or any fmt.Stringer. paraStyle is an optional named paragraph style from the template document.
func (*RichTextParagraph) String ¶
func (rp *RichTextParagraph) String() string
String implements fmt.Stringer.
type Subdoc ¶
type Subdoc struct {
// contains filtered or unexported fields
}
Subdoc wraps an existing .odt file so it can be embedded into a Template.
Create instances via Template.NewSubdoc():
sd, err := tpl.NewSubdoc("chapter.odt")
tpl.Render(map[string]any{"chapter": sd})
In the master template (block-level insertion, strips surrounding <text:p>):
{{p .chapter }}
Automatic styles from the sub-document are renamed with a unique prefix and injected into the master document to avoid collisions. Images inside the sub-document are copied into the output archive.
type Template ¶
type Template struct {
IsRendered bool
// contains filtered or unexported fields
}
Template is the central object. It holds the original ODT bytes and accumulates rendering state. A single Template may be rendered multiple times with different contexts.
func NewFromReader ¶
NewFromReader loads an ODT template from an io.Reader.
func (*Template) ContentXML ¶
ContentXML returns the raw content.xml from the template (before patching).
func (*Template) NewSubdoc ¶
NewSubdoc creates a new Subdoc associated with tpl. If path is non-empty, the ODT file at that path is loaded.
func (*Template) Render ¶
Render renders the template with the supplied context map and stores the result internally. Call SaveTo or Save afterwards to write the output.
type TextProps ¶
type TextProps struct {
Bold bool
Italic bool
Underline string // "" = none, "solid", "dotted", etc.
Strike bool
Color string // hex color with or without leading "#"
Size float64 // font size in points, 0 = unset
Font string // font family, "" = unset
Superscript bool
Subscript bool
}
TextProps holds character formatting properties for a RichText run.