I want to parse all of the templates in the same template.Template
struct but I don't know how and also it gives me an error. I have the next code:
package main
import (
"embed"
"html/template"
"log"
"os"
)
//go:embed internal/web/views/*
var viewsFS embed.FS
func main() {
tmpls, err := template.New("").ParseFS(viewsFS, "**/*.html")
if err != nil {
log.Fatal(err) // Debugging I finded out that the error is here so the code below is irrelevant
}
tmpls.ExecuteTemplate(os.Stdout, "pages/home", nil)
}
The error that gives me the ParseFS
method is the next:
$ 2023/09/16 23:36:42 template: pattern matches no files: `**/*.html`
I think that the error is in the patterns
parameter of the ParseFS
method I don't know.
I have many html files inside of the internal/web/views
directory, in fact all of the files inside that folder are html files, each one having one or more {{define}}
-type templates.
I will appreciate any help thank you
@Charlie-Tumahai credits to him for this documentation (Official Go Package website) about Glob patterns in Go
So in order to parse all of my templates in the same template.Template
struct I had to do the next:
package main
import (
"embed"
"html/template"
"log"
"os"
)
//go:embed internal/web/views/*
var viewsFS embed.FS
func main() {
tmpls, err := template.New("").
ParseFS(viewsFS,
"internal/web/views/*/*.html",
"internal/web/views/*/*/*.html",
/* Add more `*` if you have templates that are more nested */
)
if err != nil {
log.Fatal(err)
}
tmpls.ExecuteTemplate(os.Stdout, "pages/home", nil)
}
What I did was research more about Glob patterns and learn how to use it in Go, Glob patterns in Go are made in a different way that in any other language.