How to use a field of struct or variable value as template name?
Asked Answered
C

1

14

We can define template name via {{define "home"}}, and then load it in other (parent) template via {{template "home"}}.

How I can load template via variable value {{template .TemplateName}}. Or it's impossible?

Cryptocrystalline answered 3/3, 2015 at 11:21 Comment(0)
C
29

Unfortunately you can't.

The syntax of the {{template}} action:

{{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 which could vary during execution based on parameters.

If the allowed syntax would be:

{{template pipeline}}

then you could use something like {{template .TemplName}} but since the syntax only allows a constant string, you can't.

Reasoning from Rob why dynamic template invocation is not allowed (source):

We want the template language to be statically analyzable so the context of a template's invocation is clear, checkable, and lockdownable. If an invocation point is totally dynamic, this can't be done. Similarly, if a template can belong to multiple sets, its context can differ between sets in a way that would require all sets to be analyzed simultaneously. Since both these constraints are easy to work around if you want to, at the cost of losing those static checks in a higher-level package, it seemed wise to control the situation in the base template implementation. A higher-level package, such as a hypothetical HTML-only wrapper, can guarantee no workarounds more easily if the constraints are clear.

Alternative #1: Execute Includable Template First

What you can do is execute the template you would want to include first, and insert the result where you want to include it. You can use special types not to escape the result of the inner template when inserting, for example html.HTML in 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 unescaped. If you leave out template.HTML:

params.Value = b.String()

t1 would be inserted escaped, like this:

<html><body>
Now I will include template with name: t1
I&#39;m template &lt;b&gt;t1&lt;/b&gt;.
</body>/html>

Alternative #2: Restructure Templates

You can restructure your templates not to be in situations where you would want to include a template with varying names.

Example: you might want to create pages where you have a page template something like this:

<html><body>
    Title, headers etc.
    {{template .Page}}
    Footers
</body></html>

You can restructure it to be something like this:

header template:

<html><body>
    Title, headers, etc.

footer template:

    Footers
</body></html

And your page templates would include header and footer like this:

{{template "header" .}}
    Page content comes here.
{{template "footer" .}}

Alternative #3: Use {{if}} action and predefined names

If you know the template names prior and it is not an exhausting list, you can use the {{if}} template action to include the desired template. Example:

{{if eq .Name "page1"}}

    {{template "page1" .}}

{{else if eq .Name "page2"}}

    {{template "page2" .}}
    ...

{{end}}

Alternative #4: Modifying the static template text

The idea here is that you could modify the static text of the outer template manually and insert the name of the inner template you want to include.

The downside of this method is that after inserting the name of the inner template, you have to re-parse the template, so I don't recommend this.

Coz answered 3/3, 2015 at 11:52 Comment(4)
Thanks, @icza. Really, I'm panicking now :). I thought easy way exists. Your variants are very good, but in my situation I prefer using like this {{ if .IsArticle }}{{ template "article" . }}{{ else }}{{ template "page" .}}{{ end }}. Where IsArticle bool flag based on user request (URL).Cryptocrystalline
@Cryptocrystalline That is also a very good alternative if you know the template names prior and it is not an exhausting list. I include it in the answer if you don't mind.Coz
Thank you, two years later I have asked myself how to deal with this situation and it really seems that using a bunch of {{ if }} is really the way to go...Nyeman
Strange that it still doesnt let me do ``` { "upstreams": [ {{- range $key, $value := .services }} {{ template $key . }} {{- end }} ] } ```Little

© 2022 - 2024 — McMap. All rights reserved.