Answer the question
In order to leave comments, you need to log in
Where is the templates error?
I recently started learning about Go.
And now I'm dealing with templates.
go:
package main
import(
"log"
"net/http"
"html/template"
)
func hello( res http.ResponseWriter, req *http.Request ) {
res.Header().Set(
"Content-Type",
"text/html",
)
t, err := template.ParseFiles( "templates/content.html" )
if err != nil {
log.Println( err );
}
t.Execute( res, nil )
}
func main() {
http.HandleFunc( "/", hello )
http.ListenAndServe(":60", nil )
}
{{ template "templates/header.html" . }}
text
{{ template "templates/footer.html" . }}
<!DOCTYPE html>
<html>
<head>
<title>text</title>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>text</title>
</head>
<body>
text
</body>
</html>
Answer the question
In order to leave comments, you need to log in
In order for a template to be executed, it needs to be parsed, defined, and named.
It is not enough to specify the path to the file
{{ template "templates/header.html" . }} //<-вот здесь ошибка
{{ template "header" . }}
text
{{ template "footer" . }}
{{define "header"}}
<!DOCTYPE html>
<html>
<head>
<title>text</title>
</head>
<body>
{{end}}
{{define "footer"}}
</body>
</html>
{{end}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question