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