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

Use templates from go-bindata? #23

Open
cytec opened this issue Feb 7, 2015 · 5 comments
Open

Use templates from go-bindata? #23

cytec opened this issue Feb 7, 2015 · 5 comments

Comments

@cytec
Copy link

cytec commented Feb 7, 2015

Is there any way to use templates from go-bindatas BinaryFileSystem instead of loading them from disk?

@rogierlommers
Copy link

I'm interested to.

1 similar comment
@aimuzov
Copy link

aimuzov commented Aug 2, 2015

I'm interested to.

@cytec
Copy link
Author

cytec commented Aug 2, 2015

i've got it working this way:

//load templates from the asset that go-bindata provides
func loadTemplates(list ...string) *template.Template {

    for _, x := range list {
        templateString, err := Asset("assets/templates/" + x)
        if err != nil {
            log.Fatal(err)
        }

        // get file contents as string
        _, err = templates.New(x).Parse(string(templateString))
        if err != nil {
            log.Fatal(err)
        }
    }

    return templates
}

tmpl := loadTemplates("_script.tmpl", "_header.tmpl", "calendar.tmpl", "addSeries.tmpl", "history.tmpl", "shows.tmpl", "show.tmpl", "settings.tmpl", "presets.tmpl")
router.SetHTMLTemplate(tmpl)

@mzupan
Copy link

mzupan commented Jan 16, 2016

@cytec sorry new to Go but where is templates inited to deal with the return

@hryamzik
Copy link

hryamzik commented Mar 5, 2016

func loadTemplates(list ...string) multitemplate.Render {
    r := multitemplate.New()

    for _, x := range list {
        templateString, err := Asset("templates/" + x)
        if err != nil {
            log.Fatal(err)
        }

        tmplMessage, err := template.New(x).Parse(string(templateString))
        if err != nil {
            log.Fatal(err)
        }

        r.Add(x, tmplMessage)
    }

    return r
}

func main() {
    router := gin.Default()
    router.HTMLRender = loadTemplates("index.tmpl","_script.tmpl", "_header.tmpl")
    router.GET("/", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl", gin.H{
            "title": "Main website",
        })
    })
}

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

5 participants