-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_templates.go
83 lines (67 loc) · 2.78 KB
/
gf_templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
GloFlow application and media management/publishing platform
Copyright (C) 2019 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_core
import (
"fmt"
"os"
"strings"
"path/filepath"
"io/ioutil"
"text/template"
)
//-------------------------------------------------
func TemplatesLoad(pMainTemplateFilepathStr string,
pRuntimeSys *RuntimeSys) (*template.Template, []string, *GFerror) {
templateFilenameStr := filepath.Base(pMainTemplateFilepathStr)
templateDirPathStr := filepath.Dir(pMainTemplateFilepathStr)
//---------------------
// SUB_TEMPLATES - templates that are imported into the main template
subtemplates_dir_path_str := fmt.Sprintf("%s/subtemplates", templateDirPathStr)
subtemplatesNamesLst := []string{}
subtemplates_file_paths_lst := []string{}
// load subtemplates if the subtemplates/ dir exists
if _, err := os.Stat(subtemplates_dir_path_str); !os.IsNotExist(err) {
files_lst, err := ioutil.ReadDir(subtemplates_dir_path_str)
if err != nil {
gfErr := ErrorCreate("failed to parse a template",
"dir_list_error",
map[string]interface{}{"subtemplates_dir_path_str": subtemplates_dir_path_str,},
err, "gf_core", pRuntimeSys)
return nil, nil, gfErr
}
for _, f := range files_lst {
filename_str := f.Name()
if strings.HasSuffix(filename_str, ".html") {
subtemplatesNamesLst = append(subtemplatesNamesLst, strings.Split(filename_str, ".")[0])
subtemplates_file_paths_lst = append(subtemplates_file_paths_lst, fmt.Sprintf("%s/%s", subtemplates_dir_path_str, filename_str))
}
}
}
//---------------------
// TEMPLATES
templatesPathsLst := append([]string{pMainTemplateFilepathStr,}, subtemplates_file_paths_lst...)
// IMPORTANT!! - load several template files into a single template name
mainTmpl, err := template.New(templateFilenameStr).ParseFiles(templatesPathsLst...)
if err != nil {
gfErr := ErrorCreate("failed to parse a template",
"template_create_error",
map[string]interface{}{"main_template_filepath_str": pMainTemplateFilepathStr,},
err, "gf_core", pRuntimeSys)
return nil, nil, gfErr
}
//---------------------
return mainTmpl, subtemplatesNamesLst, nil
}