2

I've the below code:

package main

import (
    "fmt"
    "html/template"
    "log"
    "net/http"
    "onsen/resources"
)

var view *template.Template
var err error

func init() {
    fmt.Println("Starting up.")
    view = template.Must(template.ParseFS(resources.Views, "templates/layouts/*.html", "templates/views/*.html", "templates/partials/*.html"))
    if err != nil {
        log.Fatal("Error loading templates:" + err.Error())
    }
}

func main() {
    server := http.Server{
        Addr: "127.0.0.1:8070",
    }

    http.Handle("/webUI/", http.StripPrefix("/webUI/", http.FileServer(http.FS(resources.WebUI))))

    http.HandleFunc("/process", process)
    http.HandleFunc("/home", home)
    http.HandleFunc("/test", test)

    server.ListenAndServe()
}

Where onsen/resources is:

package resources

import (
    "embed"
)

// Views is our static web server layouts, views with dynamic content and partials content that is a static view.
//go:embed templates/layouts templates/views templates/partials
var Views embed.FS

And the routes functions are:

package main

import (
    "log"
    "net/http"
)

func home(w http.ResponseWriter, r *http.Request) {
    err = view.ExecuteTemplate(w, "home.html", nil)
    if err != nil {
        log.Fatalln(err)
    }
}

func test(w http.ResponseWriter, r *http.Request) {
    err = view.ExecuteTemplate(w, "test.html", nil)
    if err != nil {
        log.Fatalln(err)
    }
}

func process(w http.ResponseWriter, r *http.Request) {
    err = view.ExecuteTemplate(w, "process.html", nil)
    if err != nil {
        log.Fatalln(err)
    }
}

My templates are: base.html

<!-- Content of base.html: -->
{{define "base"}}
<!doctype html>
 <html lang="en">
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta charset="utf-8">
 </head>
 <body>
            {{template "content" .}}
</body>
</html>
{{end}}

And the views are:

<!-- Content of home.html: -->
{{template "base" .}}
{{define "content"}}
This is home
{{end}}

And;

<!-- Content of test.html: -->
{{template "base" .}}
{{define "content"}}
test file is here
{{end}}

And

<!-- Content of process.html: -->
{{template "base" .}}
{{define "content"}}
process goes here
{{end}}

But ALL the routes are showing the same result, which is same as template test.html

enter image description here

I referred to this for my template structure, but looks something related to embed!

Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
  • 2
    The application parses all of the template files to one template set. The "content" template in test.html is the last "content" template to be added to the set. That's the one you see for each page. Parse the template for each page to a separate set. Here's an example: `test = template.Must(template.ParseFS(resources.Views, "templates/layouts/*.html", "templates/views/test.html"))` – Charlie Tumahai Oct 22 '21 at 21:05
  • Thanks @CeriseLimón, it is ok now. – Hasan A Yousef Oct 23 '21 at 06:43

1 Answers1

0

Thanks to the comment by Cerise

The application parses all of the template files to one template set. The "content" template in test.html is the last "content" template to be added to the set. That's the one you see for each page. Parse the template for each page to a separate set.

So, here the correct working code

package main

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

func process(w http.ResponseWriter, r *http.Request) {
    view := template.Must(template.ParseFS(resources.Views, "templates/layouts/base.html", "templates/views/other.html", "templates/partials/*.html"))

    type approval struct {
        Status bool
    }

    workflow := approval{Status: false}
    err = view.ExecuteTemplate(w, "process.html", workflow)
    if err != nil {
        log.Fatalln(err)
    }
}

And template process.html is:

<!-- Content of other.html: -->
{{template "base" .}}
{{define "content"}}
process goes here
 {{ .Status }}

     {{if .Status}} 
        {{template "approved"}}
    {{else}} 
        {{template "rejected"}}
    {{end}}

{{end}}
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203