更新嵌套模板Golang

I am trying to change contents dynamically. But the content remains the same. Seems to fetch first match. no matter what the template is. Does not work even with hardcoded file names. The code works as expected but the content cannot be changed.

Main layout

{{define "layout"}}
    <html>
    <body>
        {{ template "content" }}
    </body>
    </html>
{{end}}

Sub template 1

{{ define "content" }}

<h1 style="color: red;">Page 1!</h1>

{{ end }}

Sub template 2

{{ define "content" }}

<h1 style="color: blue;">Page 2!</h1>

{{ end }}

The Go code

package main

import (
    "html/template"
    "net/http"
    "strings"
)

var tpl *template.Template

func init() {
    tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}

func main() {
    http.HandleFunc("/", index)
    http.ListenAndServe(":8080", nil)
}

func index(w http.ResponseWriter, r *http.Request) {

    path := strings.Trim(r.URL.Path, "/")
    switch path {
    case "":
        path = ("index.gohtml")
    default:
        path = (path + ".gohtml")
    }

    err := tpl.ExecuteTemplate(w, "layout", path)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}

I have also tried to ParseFiles before Execute with no luck. What am I doing wrong?

Adjusting the path after the templates have been parsed is too late, I believe.

What could work (though I'm not sure is the most elegant solution here) is using the AddParseTree method:

AddParseTree adds parse tree for template with given name and associates it with t. If the template does not already exist, it will create a new one. If the template does exist, it will be replaced.

Applied to your case, based on the condition you would Parse the relevant template file (sub template 1 or 2), and then add it with AddParseTree to tpl, before you execute it.

At last I got it to work, but only when not following the manuals.

Solution Part 1

Skip {{define}} and {{end}} in templates. Weird...

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Go Web Programming</title>
  </head>
  <body>
    layout level
    {{ template "content" . }}
  </body>
</html>

And in subtemplates as well...

<h1 style="color: red;">Page 1!</h1>

Solution Part 2

I found a code snippet with AddParsTree as Eli mentioned and here is the code (simplified with no error handling)

package main

import (
    "html/template"
    "net/http"
    "strings"
)

var tpl *template.Template

func init() {
    tpl = template.Must(template.ParseGlob("templates/*.html"))
}

func main() {
    http.HandleFunc("/", index)
    http.ListenAndServe(":8080", nil)
}

func index(w http.ResponseWriter, r *http.Request) {

    path := strings.Trim(r.URL.Path, "/")
    switch path {
    case "":
        path = ("home.html")
    default:
        path = (path + ".html")
    }

    layout := tpl.Lookup("layout.html")
    layout, _ = layout.Clone()
    t := tpl.Lookup(path)
    _, _ = layout.AddParseTree("content", t.Tree)
    layout.Execute(w, "")

I do not really understand why I have to disobey the manuals to get it to work. Any comments that enlighten me will be appreciated.