'include' needs to be defined in the scope of a 'tpl' template as well as regular file-loaded templates.
(t *template.Template, includedNames map[string]int)
| 130 | // 'include' needs to be defined in the scope of a 'tpl' template as |
| 131 | // well as regular file-loaded templates. |
| 132 | func includeFun(t *template.Template, includedNames map[string]int) func(string, any) (string, error) { |
| 133 | return func(name string, data any) (string, error) { |
| 134 | var buf strings.Builder |
| 135 | if v, ok := includedNames[name]; ok { |
| 136 | if v > recursionMaxNums { |
| 137 | return "", fmt.Errorf( |
| 138 | "rendering template has a nested reference name: %s: %w", |
| 139 | name, errors.New("unable to execute template")) |
| 140 | } |
| 141 | includedNames[name]++ |
| 142 | } else { |
| 143 | includedNames[name] = 1 |
| 144 | } |
| 145 | err := t.ExecuteTemplate(&buf, name, data) |
| 146 | includedNames[name]-- |
| 147 | return buf.String(), err |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // As does 'tpl', so that nested calls to 'tpl' see the templates |
| 152 | // defined by their enclosing contexts. |
no test coverage detected
searching dependent graphs…