samedi 2 novembre 2019

Why doesn't my template get rendered in the browser?

I want to render a template to the browser. However, nothing shows up. Here's the code I'm running, main.go:

    package main

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


    func main() {
        mux := http.NewServeMux()
        mux.HandleFunc("/", home)
        log.Fatal(http.ListenAndServe("0.0.0.0:8000", mux))
    }


    func home(w http.ResponseWriter, r *http.Request) {
        data := pageData{"MyTabTitle", "MyPageName"}

        tmpl := template.Must(template.ParseFiles("base.html"))
        tmpl.Execute(w, data)
    }


    type pageData struct {
        tabTitle, pageName string
    }

In the same directory I also have the base.html-file

    <!doctype html>
    <html>
        <head>
            <title> 
        </head>
        <body>
            <header>
                <h1>  </h1>
            </header>
        </body>
    </html>



Aucun commentaire:

Enregistrer un commentaire