-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Organize a decent template system #78
Comments
Here is my template loader if it helps... i have multiple subfolders and one master page. works well for me so far. please see if it helps /*
Templates visitor for gin
*/
var (
templateFiles []string
rootPath string
templates *template.Template
templateDelims []string
)
func visit(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
// don't process folders themselves
if f.IsDir() {
/*if templates == nil {
templates = template.New(f.Name())
}else {
t, _ := templates.AddParseTree(f.Name(), templates.Tree)
templates = t
}*/
return nil
}
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
log.Println("Failed reading file:", path)
return nil
}
fileStr := string(fileBytes)
templateName := strings.Replace(path[len(rootPath):],"\\","/",-1)
//templateName := path[len(rootPath):]
log.Printf("Processing template %s\n", templateName)
if templates == nil {
templates = template.New(templateName)
templates.Delims(templateDelims[0], templateDelims[1])
_, err = templates.Parse(fileStr)
} else {
_, err = templates.New(templateName).Parse(fileStr)
}
if err!=nil{
log.Printf("Error %s\n", err)
}
//log.Printf("******Processed template %s\n", templates.Name())
return nil
}
func LoadGinTemplates(path string, engine *gin.Engine, delims []string) {
templateFiles=make([]string,0)
templateDelims=make([]string,2)
templateDelims[0] = delims[0]
templateDelims[1] = delims[1]
rootPath = path
templates = template.New("")
templates.Delims(templateDelims[0], templateDelims[1])
filepath.Walk(path, filepath.WalkFunc(visit))
engine.HTMLRender = render.HTMLRender{
Template: templates,
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this might be caused by my lack of experience or the way
html/template
works, but I cannot figure how I could get:In my ideal world:
base.tmpl
I have tried something very similar to what was described in #27, but haven't been able to get it to work.
Update: I got the solution described in #27 to work.
I'm not suggesting
gin
to implement from scratch a templating engine, but:html/template
only then maybe recommending a solution.The text was updated successfully, but these errors were encountered: