Whereas you’re using the html/template
, you can wow the {{template "nome do template"}}
for it to be inserted.
All "commands" can be viewed at https://golang.org/pkg/text/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.
So considering you have two files, you could do:
header.html
{{define "header"}}
<header>
Este é o cabeçalho de {{.Fulano}}
</header>
{{end}}
index.html
{{define "index"}}
{{template "header" .}}
<body>
Este é o corpo de {{.Fulano}}
</body>
{{end}}
No Golang could use:
package main
import (
"html/template"
"os"
)
func main() {
t, err := template.ParseGlob(`path\to\*.html`)
if err != nil {
panic(err)
}
type Exemplo struct {
Fulano string
}
if err = t.ExecuteTemplate(os.Stdout, "index", &Exemplo{Fulano: "Sicrano"}); err != nil {
panic(err)
}
}
That would result in:
<header>
Este é o cabeçalho de Sicrano
</header>
<body>
Este é o corpo de Sicrano
</body>
Note that the {{define}}
to name each "file". In the second case, the index.html
, is used the {{template "header" .}}
and this is intended to inject the content that has been defined using {{define "header"}}
.
Finally, to inject one another use the {{define "nome}}
and then use the {{template "nome"}}
to insert the content. In the above case, it is used {{template "nome" .}}
(note the .
at the end) to allow the "header" to have access to the Exemplo
and read the Fulano
.
Depends, which lib is using the native HTTP lib? or is using a framework?
– Guilherme Nascimento