I have a layout template (base.tmpl) that I would like to use several times in login page (login.tmpl), main content page (main.tmpl), and so on.
base.tmpl file:
<html>
<head>
<title>Untitled</title>
</head>
<body>
<div id="main-content">
</div>
</body>
</html>
The pages have a first directive to use the base layout and they can define header, content and footer blocks.
login.tmpl file:
<title>Login</title>
<link rel="stylesheet" href="styles.css">
<input type="username">
<input type="submit" value="Login">
main.tmpl file:
<title>Main content</title>
<h1>Welcome</h1>
<script>console.log("ok");</script>
Loading and render template functions:
func LoadTemplates(files []string) {
templates = template.Must(template.New("myTemplates").ParseFiles(files...))
}
func Render(w http.ResponseWriter, tmpl string, data interface{}) {
templates.ExecuteTemplate(w, tmpl, data)
}
Using my code to render login page:
// the following line is a simplification, actually I load all files together only once.
LoadTemplates(string[]{"base.tmpl", "login.tmpl", "main.tmpl"})
Render(w, "login.tmpl", struct{}{})
Using my code to render main content page:
// the following line is a simplification, actually I load all files together only once.
LoadTemplates(string[]{"base.tmpl", "login.tmpl", "main.tmpl"})
Render(w, "login.tmpl", struct{}{})
The problem is that I content block is overwritten because it is two times. So, if I render login or main page, I see the same.
Aucun commentaire:
Enregistrer un commentaire