Full source code: https://github.com/remoteworkerid/backendgolang/blob/feature/RWIDPF-13/server.go
My code works for DEV_MODE==true. But it doesn't for DEV_MODE==false.
PrecompileTemplate:
func precompileTemplate() {
// Menggunakan template.ParseGlob untuk memuat semua file template
templates = template.Must(template.ParseGlob("static/*.html"))
templateNames := templates.Templates()
fmt.Println("Nama-nama template yang terdaftar:")
for _, t := range templateNames {
fmt.Println(t.Name())
}
}
HomeHandler:
if devMode {
tmpl, err := template.ParseFiles("static/base.html", "static/daftar.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.ExecuteTemplate(w, "base.html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
log.Println("Production code daftar")
err = templates.ExecuteTemplate(w, "base.html", data)
if err != nil {
log.Println("Ada error di daftar")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
My base.html excerpt:
<div id="content" class="p-5">
{{block "content" .}}
</div>
My daftar.html (full):
{{ define "content" }}
<h1>Daftar RWID</h1>
{{ end }}
How do you render a precompiled template for subtemplate case? I kinda expecting the ExecuteTemplate will accept three parameter: base, sub and data. But as its' only one.. I am guessing daftar.html (a subtemplate). But, in daftar.html I didn't mention the superclass..