Read returns the template with the given name, optionally including partials. Partials are loaded from the 'partial' subdirectory and defined as named blocks.
(name string, partials ...string)
| 15 | // Read returns the template with the given name, optionally including partials. |
| 16 | // Partials are loaded from the 'partial' subdirectory and defined as named blocks. |
| 17 | func (tr *TemplateReader) Read(name string, partials ...string) string { |
| 18 | var prefix string |
| 19 | if len(partials) > 0 { |
| 20 | partialDefs := make([]string, 0, len(partials)) |
| 21 | for _, partial := range partials { |
| 22 | content, err := fs.ReadFile(tr.FS, path.Join("templates", "partial", partial+".go.tpl")) |
| 23 | if err != nil { |
| 24 | panic(fmt.Sprintf("failed to read partial template %s: %v", partial, err)) |
| 25 | } |
| 26 | // Normalize line endings |
| 27 | contentStr := strings.ReplaceAll(string(content), "\r\n", "\n") |
| 28 | partialDefs = append(partialDefs, |
| 29 | fmt.Sprintf("{{- define \"partial_%s\" }}\n%s{{- end }}", partial, contentStr)) |
| 30 | } |
| 31 | prefix = strings.Join(partialDefs, "\n") |
| 32 | } |
| 33 | content, err := fs.ReadFile(tr.FS, path.Join("templates", name)+".go.tpl") |
| 34 | if err != nil { |
| 35 | panic(fmt.Sprintf("failed to load template %s: %v", name, err)) |
| 36 | } |
| 37 | // Normalize line endings to ensure consistent template parsing across platforms |
| 38 | contentStr := strings.ReplaceAll(string(content), "\r\n", "\n") |
| 39 | if prefix != "" { |
| 40 | return prefix + "\n" + contentStr |
| 41 | } |
| 42 | return contentStr |
| 43 | } |
no outgoing calls
no test coverage detected