I've setup my handlers with little to no issue.
At the moment I've got an index.html page - which I've defined as "index", and I've got a login.html page - which is just a dummy form.
I essentially want to navigate to the /login url (already setup) and it display the index.html (base page) layout - with the form on the page.
I thought I could simply parse in {{template. index}} into my login.html file, but that doesn't seem to be working. I've spent the last two hours trying to resolve this - scratching my head but it's not clicking for me at the moment.
I've also added in the handlers.go file, just incase the global parsing is causing an issue.
index.html
~~~
{{ define "index" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/static/css/main.css" />
<title>Document</title>
</head>
<body>
<div class="wrapper">
{{template "nav" .}}
<div class="bodySection">
</div>
</div>
</body>
</html>
{{ end }}
login.html
~~~
{{ define "login" }}
<form action="login" method="post">
<input type="hidden" name="goto" value="news">
<table>
<tbody>
<tr>
<td>username:</td>
<td><input type="text" name="acct" size="20" autocorrect="off" spellcheck="false" autocapitalize="off" autofocus></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="pw" size="20"></td>
</tr>
</tbody>
</table>
<br>
<input type="submit" value="login">
</form>
{{ end }}
handlers.go
~~~
package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
/* sort out 404
if r.ULR.Path =! "/"{
http.NotFound(w, r)
return
}
*/
// package level variable
var tmpl *template.Template
func templateParse() {
var err error
tmpl, err = template.ParseGlob("ui/html/*.html")
if err != nil {
log.Println("Error parsing templates:", err)
// nmaybe make fatal error
return
}
for _, t := range tmpl.Templates() {
fmt.Println("Parsed template:", t.Name())
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
err := tmpl.ExecuteTemplate(w, "index", nil)
if err != nil {
log.Println("Template execution error:", err)
}
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
err := tmpl.ExecuteTemplate(w, "login", nil)
if err != nil {
log.Println("Template execution error:", err)
}
}
Although it would be helpful if you add more data like what error you are facing and how it is not working, You can simply define a base template then extend it in two pages:
index.html
{{ define "login" }}
{{template "base" .}}
{{ end }}
login.html
{{ define "login" }}
{{template "base" .}}
{{ end }}
{{ define "content" }}
< -- login content -- >
</form>
{{ end }}
but you should write and define base before hand. if there is other data that can help I would be happy to know.