These are chat archives for gin-gonic/gin

17th
Jun 2015
DenisNeustroev
@DenisNeustroev
Jun 17 2015 15:50

@manucorporat what about "contrib/renders/multitemplate" and template's "Funcs(someFuncMap)"?

func LoadTemplates(templatesDir string) *multitemplate.Render {
    r := multitemplate.New()

     funcMap := template.FuncMap{
         "shorten": func(str string) string {
             if len(str) > 140 {
                 return str[:140]
             }
             return str
         },
     }

    layouts, err := filepath.Glob(templatesDir + "/layouts/*.html") // base.html, view.html
    if err != nil {
        panic(err.Error())
    }

    views, err := filepath.Glob(templatesDir + "/*.html") // index.html, about.html and etc.
    if err != nil {
        panic(err.Error())
    }

    for _, view := range views {
        files := append(layouts, view)
        name := filepath.Base(view) // index.html, about.html
        r.Add(name, template.Must(template.New(name).Funcs(funcMap).ParseFiles(files...)))
    }
    return &r
}
c.HTML(http.StatusOK, "index.html", gin.H{
        "test": "test",
}) // render nothing

It will compile but GET request return a empty page, maybe someone knows a solution?