InitTemplates loads all template files from the configured parent dir.
(cfg *config.Config)
| 118 | |
| 119 | // InitTemplates loads all template files from the configured parent dir. |
| 120 | func InitTemplates(cfg *config.Config) error { |
| 121 | log.Info("Loading templates...") |
| 122 | tmplFiles, err := os.ReadDir(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir)) |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | for _, f := range tmplFiles { |
| 128 | if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") { |
| 129 | parts := strings.Split(f.Name(), ".") |
| 130 | key := parts[0] |
| 131 | initTemplate(cfg.Server.TemplatesParentDir, key) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | log.Info("Loading pages...") |
| 136 | // Initialize all static pages that use the base template |
| 137 | err = filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, pagesDir), func(path string, i os.FileInfo, err error) error { |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") { |
| 142 | key := i.Name() |
| 143 | initPage(cfg.Server.PagesParentDir, path, key) |
| 144 | } |
| 145 | |
| 146 | return nil |
| 147 | }) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | |
| 152 | log.Info("Loading user pages...") |
| 153 | // Initialize all user pages that use base templates |
| 154 | err = filepath.Walk(filepath.Join(cfg.Server.TemplatesParentDir, templatesDir, "user"), func(path string, f os.FileInfo, err error) error { |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | if !f.IsDir() && !strings.HasPrefix(f.Name(), ".") { |
| 159 | corePath := path |
| 160 | if cfg.Server.TemplatesParentDir != "" { |
| 161 | corePath = corePath[len(cfg.Server.TemplatesParentDir)+1:] |
| 162 | } |
| 163 | parts := strings.Split(corePath, string(filepath.Separator)) |
| 164 | key := f.Name() |
| 165 | if len(parts) > 2 { |
| 166 | key = filepath.Join(parts[1], f.Name()) |
| 167 | } |
| 168 | initUserPage(cfg.Server.TemplatesParentDir, path, key) |
| 169 | } |
| 170 | |
| 171 | return nil |
| 172 | }) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | |
| 177 | return nil |
no test coverage detected