template.ParseFiles reading multiple files


I am using template.ParseFiles to read files but the problem is the list of templates is going bigger day by day and I need all of them to be loaded.

I need to do something like:
var temp_pages = template.Must(template.ParseFiles("/temps1/*.html","/temps2/*.html")

/temps1/* == will read all the html files in temps1 folder
/temps1/* == will read all the html files in temps2 folder

I know I can use ParseGlob but using that I can access only one folder.


I figured it out.

  1. Read all files of different directories using ReadDir. Created a function:

     func GetTempFilesFromFolders(folders []string) []string {
     var filepaths []string
     for _, folder := range folders {
     	files, err := ioutil.ReadDir(folder)
     	for _, file := range files {
     		if(strings.Contains(file.Name(),".html")) {
     			filepaths = append(filepaths, folder+file.Name())
     return filepaths
  2. Use the string slice as variadic parameter: filepaths…

     tempfiles := funcs.GetTempFilesFromFolders(dirs)
     var temp_lp = template.Must(template.ParseFiles(tempfiles...))

Try something like this:

t, err := template.New("templ").ParseGlob(path + "/templates/*.html")

[le] I saw you already used ParseGlob.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.