2024-04-23 02:47:53 -04:00
|
|
|
package common
|
2023-09-12 06:40:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoadTemplate 加载模板
|
2023-09-16 09:46:59 -04:00
|
|
|
// templates 模板文件名
|
2023-09-13 01:47:22 -04:00
|
|
|
func LoadTemplate(template string) ([]byte, error) {
|
2023-09-12 06:40:24 -04:00
|
|
|
tPath := filepath.Join("templates", template)
|
|
|
|
if _, err := os.Stat(tPath); err == nil {
|
|
|
|
file, err := os.Open(tPath)
|
|
|
|
if err != nil {
|
2023-09-13 01:47:22 -04:00
|
|
|
return nil, err
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
|
|
|
defer func(file *os.File) {
|
2023-09-22 13:31:04 -04:00
|
|
|
if file != nil {
|
|
|
|
_ = file.Close()
|
|
|
|
}
|
2023-09-12 06:40:24 -04:00
|
|
|
}(file)
|
|
|
|
result, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
2023-09-13 01:47:22 -04:00
|
|
|
return nil, err
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
2023-09-13 01:47:22 -04:00
|
|
|
return result, nil
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|
2023-09-13 01:47:22 -04:00
|
|
|
return nil, errors.New("模板文件不存在")
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|