CloudyKit / jet

Jet template engine
Apache License 2.0
1.26k stars 106 forks source link

go:embed Assets - load tempaltes from Assets #195

Closed replay111 closed 3 years ago

replay111 commented 3 years ago

Hi,

I am totally new with go, but I am trying to prepare simple web page and I want to embed tempaltes files to final binary. My question is how to do that ;-)

I've prepared someting like:


//go:embed templates
var assets embed.FS

// and passes it to my application config:
app.TemplatesFS = assets

but I have no idea how to use it with jet templates.

I am using jet with files on file system:

var view = jet.NewSet(
    jet.NewOSFileSystemLoader("./templates"),
    jet.InDevelopmentMode(), // remove in production
)

then I have function to render:

func TemplateRender(w http.ResponseWriter, tplName string, data jet.VarMap) error {

    t, err := view.GetTemplate(tplName)
    if err != nil {
        log.Printf("Template not found or other error ocurred: %v", err)
        return err
    }

    err = t.Execute(w, data, nil)
    if err != nil {
        log.Printf("Error execution template: %v", err)
        return err
    }

    return nil
}

and this is working fine, but when I move compiled app to some other place I do not have access to my html files :(

Can someone show me (in a very simple code ;-) :-) ) how to do that - this would be some start for me

thanks!

sauerbraten commented 3 years ago

This is a bit of an open issue, basically you have to write an adapter between Go's fs.FS and jet's Loader interface. See #191 for more details. A workaround is to just use http.FS, which Jet already knows how to use.

I'll close this because it's basically the same feature request as #191, but feel free to reply here or reopen when the information in #191 doesn't completely solve your problem.

replay111 commented 3 years ago

Thanks for replay ;-) I have seen this issue but, as I mentioned I am starting learning go, I do not know how to pass this loader to Jet Engine ;-) But I will try to experiment with it - maybe I will find the way ;-)