template inhereting issues: one template overrides the other

Viewed 157

I have two HTML templates (login.html, signup.html) and they both inherit the (base.html) file, the problem is that one of the two files is overriding the title and content of the other. Meaning that when I go to /login/ I see the login content, but when i go to /signup/ I see again the Login content! and when i stop (login.html) from extending the base.html the /signup/ shows me the content of the signup page.

I'm still new to Go, building a todo app, so what am I doing wrong here?

base.html:

{{define "base"}}
<!DOCTYPE html>
...
<title>{{template "title"}}</title>
...
    {{template "main" .}}
...
{{end}}

login.html:

{{template "base" .}}
{{define "title"}}Login{{end}}
{{define "main"}}
     Login Now!
{{end}}

Till here everything is working fine
Now when i started to add more templates:
signup.html

{{template "base" .}}
{{define "title"}}Sign Up{{end}}
{{define "main"}}
    Login or miss who you love >:(
{{end}}

Update: my Go code

main.go (resposible for routing and reusable functions):

package main

import (
    "html/template"
    "log"
    "net/http"

    "github.com/julienschmidt/httprouter"
)

var templates = template.Must(template.ParseFiles("tmpl/base.html", "tmpl/index.html", "tmpl/signup.html", "tmpl/login.html"))

func renderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, data interface{}) {
    err := templates.ExecuteTemplate(w, tmpl+".html", data)
    if err != nil {
        http.NotFound(w, r)
        return
    }
}

func main() {
    r := httprouter.New()
    r.GET("/signup/", signupHandler)
    r.POST("/createUser/", createUserHandler)
    r.GET("/login/", loginHandler)
    log.Fatal(http.ListenAndServe(":8080", r))
}

auth.go (Contains handlers for authentication)

package main

import (
    "net/http"

    "github.com/julienschmidt/httprouter"
)

func signupHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    renderTemplate(w, r, "signup", nil)
}
// Logins
func loginHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    renderTemplate(w, r, "login", nil)


    return
    }
    
//===================================================
//===================================================
//===================================================
//===================================================
//===================================================

Database code to create user(Not sure if this is relevant)

//===================================================
//===================================================
//===================================================
//===================================================
 func createUserHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
        if r.FormValue("password1") != r.FormValue("password2") {
            http.Redirect(w, r, "/signup/", http.StatusFound)
            return
        }
        db := connectDB(w, "TodoWoo")
        defer db.Close()
        q := `INSERT INTO users(username, password) VALUES (?, ?)`
        if _, err := db.Exec(q, r.FormValue("username"), r.FormValue("password1")); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        http.Redirect(w, r, "/login/", http.StatusFound)
    }
    func loginUserHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    
    }

driver.go (Connecting database, Not sure if this is relevant

package main

import (
    "database/sql"
    "fmt"
    "net/http"
    _ "github.com/go-sql-driver/mysql"
)

// Don't forgot to defer db.Close()
func connectDB(w http.ResponseWriter, scm string) *sql.DB {
    db, err := sql.Open("mysql", fmt.Sprintf("root:My44200444Sql@(127.0.0.1:3306)/%s?parseTime=true", scm))
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
    return db
}
1 Answers

You can do this:

base.html

{{define "base_header"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ . }}</title>
</head>
<body>
{{end}}
    
{{define "base_footer"}}
</body>
</html>
{{end}}

login.html

{{ $page_title := "Login" }}
{{ $page_footer := "login footer text" }}
{{ template "base_header" $page_title }}
Login Now!
{{ template "base_footer" $page_footer }}

signup.html

{{ $page_title := "Sign Up" }}
{{ $page_footer := "signup footer text" }}
{{ template "base_header" $page_title }}
Login or miss who you love >:(
{{ template "base_footer" $page_footer }}

Related