loadTemplate: support loading mulitple templates
This commit is contained in:
		
							parent
							
								
									6bb7689a7b
								
							
						
					
					
						commit
						97eb1fae38
					
				
					 1 changed files with 24 additions and 12 deletions
				
			
		|  | @ -1,6 +1,7 @@ | ||||||
| package frontend | package frontend | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 
 | 
 | ||||||
|  | @ -16,13 +17,18 @@ import ( | ||||||
| 
 | 
 | ||||||
| //TODO: log last update | //TODO: log last update | ||||||
| 
 | 
 | ||||||
| //loadTemplate loads a single template from statikFS and returns a template object | //loadTemplate loads a list of templates, relative to the statikFS root, and a FuncMap, and returns a template object | ||||||
| func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) { | func loadTemplate(templateNames []string, funcMap template.FuncMap) (*template.Template, error) { | ||||||
| 	tmpl := template.New(templateName).Funcs(funcMap) | 	if len(templateNames) == 0 { | ||||||
|  | 		return nil, fmt.Errorf("templateNames can't be empty") | ||||||
|  | 	} | ||||||
|  | 	tmpl := template.New(templateNames[0]).Funcs(funcMap) | ||||||
| 	statikFS, err := fs.New() | 	statikFS, err := fs.New() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	for _, templateName := range templateNames { | ||||||
| 		r, err := statikFS.Open("/" + templateName) | 		r, err := statikFS.Open("/" + templateName) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
|  | @ -32,7 +38,13 @@ func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Temp | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
| 	return tmpl.Parse(string(contents)) | 		tmpl, err = tmpl.Parse(string(contents)) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return nil, err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return tmpl, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // MakeFrontend configures the router and returns a new Frontend struct | // MakeFrontend configures the router and returns a new Frontend struct | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue