Skip to content
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

Closed
al3xandru opened this issue Jul 18, 2014 · 1 comment
Closed

Organize a decent template system #78

al3xandru opened this issue Jul 18, 2014 · 1 comment

Comments

@al3xandru
Copy link

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:

  1. an easy to maintain hierarchical template structure
  2. how to organize this so that I can easily use it from the app.

In my ideal world:

  1. I could have a base.tmpl
  2. this could be extended/customized in final templates
  3. templates would be organized in a directory hierarchy that emulates the URL structure (so over time it's easy to figure out where to look for things

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:

  1. I'd really find very helpful seeing a more realistic use of templates
  2. if this is not doable/optimal with html/template only then maybe recommending a solution.
@dfilipovic39
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants