Sorry, you canβt.
The syntax for the action is {{template}} :
{{template "name"}} The template with the specified name is executed with nil data. {{template "name" pipeline}} The template with the specified name is executed with dot set to the value of the pipeline.
The name of the template to be included is a constant string; it is not a pipeline that can change at run time based on parameters.
If the valid syntax would look like this:
{{template pipeline}}
then you can use something like {{template .TemplName}} , but since the syntax allows only a constant string, you cannot.
Reason from Rob, why dynamic template call is not allowed ( source ):
We want the template language to be statically analyzed, so the context for calling the template is clear, you can check and block it. If the dial peer is fully dynamic, this is not possible. Similarly, if a pattern can belong to a plurality of sets, its context can vary between sets so that it requires the simultaneous analysis of all sets. Since both of these restrictions are easy to circumvent if you want, at the cost of losing these static checks in a higher-level package, it would be wise to control the situation in the implementation of the basic template. A higher-level package, such as a hypothetical HTML-only wrapper, can guarantee that there are no workarounds more easily if the limitations are clear.
Alternative # 1: Performing an Embeddable Template First
What you can do is execute the template that you want to include first and insert the result into which you want to include it. You can use special types to avoid deleting the result of the internal template when pasting, for example html.HTML in the case of HTML templates.
See this example:
func main() { t := template.Must(template.New("t").Parse(t)) template.Must(t.New("t1").Parse(t1)) params := struct { Name string Value interface{} }{"t1", nil} b := bytes.Buffer{} t.ExecuteTemplate(&b, params.Name, nil) params.Value = template.HTML(b.String()) t.Execute(os.Stdout, params) } const t = `<html><body> Now I will include template with name: {{.Name}} {{.Value}} </body>/html>` const t1 = `I'm template <b>t1</b>.`
Output:
<html><body> Now I will include template with name: t1 I'm template <b>t1</b>. </body>/html>
Try it on the go playground .
The result of template t1 was inserted without saving. If you leave template.HTML :
params.Value = b.String()
t1 will be shielded, for example:
<html><body> Now I will include template with name: t1 I'm template <b>t1</b>. </body>/html>
Alternative # 2: Restructuring Patterns
You may not restructure your templates in situations where you want to include a template with different names.
Example: you can create pages where you have a page template, something like this:
<html><body> Title, headers etc. {{template .Page}} Footers </body></html>
You can rebuild it this way:
header template:
<html><body> Title, headers, etc.
footer template:
Footers </body></html
And your page templates will include header and footer as follows:
{{template "header" .}} Page content comes here. {{template "footer" .}}
Alternative # 3: Use the {{if}} action and predefined names
If you know the template names earlier, and this is not an exhaustive list, you can use the template action {{if}} to include the desired template. Example:
{{if eq .Name "page1"}} {{template "page1" .}} {{else if eq .Name "page2"}} {{template "page2" .}} ... {{end}}
Alternative # 4: Change the text of a static template
The idea here is that you can manually change the static text of the external template and insert the name of the internal template you want to include.
The disadvantage of this method is that after inserting the name of the internal template, you need to reanalyze the template, so I do not recommend this.