@@ -70,65 +70,67 @@ import (
7070
7171doc := docx.NewDocument ()
7272para , _ := doc.AddParagraph ()
73- para.AddRun ().SetText (" Hello {{name}}, welcome to {{company}}!" )
73+ run , _ := para.AddRun ()
74+ run.SetText (" Hello {{name}}, welcome to {{company}}!" )
7475
75- data := map [ string ] string {
76+ data := template. MergeData {
7677 " name" : " John" ,
7778 " company" : " Acme Corp" ,
7879}
7980
80- result , _ := template.MergeTemplate (doc, data, nil )
81- result.SaveAs (" output.docx" )
81+ err := template.MergeTemplate (doc, data)
82+ // MergeTemplate modifies the document in place
83+ doc.SaveAs (" output.docx" )
8284```
8385
8486### External Word Template with MERGEFIELDs
8587
8688``` go
8789doc , _ := docx.OpenDocument (" template.docx" )
8890
89- opts := & template.MergeOptions {
91+ opts := template.MergeOptions {
9092 OpenDelimiter : " «" ,
9193 CloseDelimiter : " »" ,
9294}
9395
94- data := map [ string ] string {
96+ data := template. MergeData {
9597 " Contact_FullName" : " Jane Doe" ,
9698 " Account_Name" : " Acme Corp" ,
9799}
98100
99- result , _ := template.MergeTemplate (doc, data, opts)
100- result.SaveAs (" merged_output.docx" )
101+ err := template.MergeTemplate (doc, data, opts)
102+ // MergeTemplate modifies the document in place
103+ doc.SaveAs (" merged_output.docx" )
101104```
102105
103106### Batch Merge
104107
105108``` go
106- doc , _ := docx.OpenDocument (" invoice_template.docx" )
107-
108- records := []map [string ]string {
109+ records := []template.MergeData {
109110 {" name" : " John Smith" , " amount" : " $1,500.00" },
110111 {" name" : " Alice Johnson" , " amount" : " $2,300.00" },
111112}
112113
113- results , _ := template.BatchMerge (" invoice_template.docx" , records, nil )
114- for i , result := range results {
115- result.SaveAs (fmt.Sprintf (" invoice_%d .docx" , i+1 ))
114+ for i , data := range records {
115+ doc , _ := docx.OpenDocument (" invoice_template.docx" )
116+ template.MergeTemplate (doc, data)
117+ doc.SaveAs (fmt.Sprintf (" invoice_%d .docx" , i+1 ))
116118}
117119```
118120
119121## Files Added
120122
121- - ` pkg/template/engine .go ` — Main template engine
123+ - ` pkg/template/doc .go ` — Package documentation
122124- ` pkg/template/merge.go ` — Core merge logic
123125- ` pkg/template/merge_test.go ` — Merge tests
124126- ` pkg/template/placeholder.go ` — Placeholder detection
125127- ` pkg/template/placeholder_test.go ` — Placeholder tests
126128- ` pkg/template/consolidate.go ` — Run consolidation
127129- ` pkg/template/consolidate_test.go ` — Consolidation tests
128- - ` pkg/template/validate .go ` — Template validation
129- - ` pkg/template/validate_test .go ` — Validation tests
130+ - ` pkg/template/format .go ` — Run formatting comparison helper
131+ - ` pkg/template/options .go ` — MergeData, MergeOptions, ValidationError types
130132- ` pkg/template/walk.go ` — Document traversal
131- - ` pkg/template/batch .go ` — Batch merge support
133+ - ` pkg/template/integration_test .go ` — Integration tests
132134- ` examples/14_mail_merge/main.go ` — Programmatic template example
133135- ` examples/15_external_template/main.go ` — External Word template example
134136
0 commit comments