package models

import (
	"html/template"
	"os"
	"path/filepath"
	"strings"
)

func loadTemplates(templatesPath string, ext string) (*template.Template, error) {
	tmpls := template.New("")

	err := filepath.Walk(templatesPath, func(path string, f os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !f.IsDir() && strings.HasSuffix(f.Name(), ext) {
			_, err = tmpls.ParseFiles(path)
			if err != nil {
				return err
			}
		}
		return nil
	})

	if err != nil {
		return nil, err
	}

	return tmpls, nil
}
