yznts / kyoto

Asynchronous frontends with Go
https://pkg.go.dev/github.com/yznts/kyoto/v3
MIT License
651 stars 28 forks source link

Using ParseFS and embed.FS #47

Closed aranw closed 3 years ago

aranw commented 3 years ago

I've been trying to figure out a pattern to use embed.FS and template.ParseFS for delivering the HTML files

Do you have any examples for this? I've been struggling with a couple of different errors and including the Funcs with the parsed templates

template: \"templates/pages.home.html\" is an incomplete or empty template

    //go:embed templates
    Templates embed.FS
    return template.Must(
        template.New("templates/pages.home.html").Funcs(kyoto.TFuncMap()).ParseFS(assets.Templates, "templates/pages.home.html"),
    )
yznts commented 3 years ago

@aranw
You need to use //go:embed templates/* to include all files. But using file extension also highly recommended: //go:embed templates/*.html. Also, I'm not sure about template naming, it's possible that you'll need to use just file name, without full path while defining template like template.New("pages.home.html"). If so, please, leave a comment under this issue, I'll extend documentation :) Don't forget that for using components (not only the page itself), you'll need to parse all needed templates. You can do it with .ParseFS(assets.Templates, "templates/*.html").
Kyoto is not well tested with embeds (but I assume that everything must to be OK). I'm not using embeding in my examples due to lack of experience. This feature was introduced not so long time ago and I didn't used it in my projects yet. From my side, It's better to provide the most basic example instead of providing incorrect/not tested one.
Anyway, I'll extend docs after some testing.

Leaving this issue opened for now, in case if you have more questions.

aranw commented 3 years ago

I tried the suggested changes and I'm still receiving the same error

template: \"templates/pages.home.html\" is an incomplete or empty template

and when I remove the Funcs(kyoto.TFuncMap()) call I get the following error

error":"template: pages.home.html:14: function \"dynamics\" not defined

If I go ahead and remove the {{ dynamics }}

I go back to having that incomplete or empty template error again

yznts commented 3 years ago

and when I remove the Funcs(kyoto.TFuncMap()) call I get the following error

Right. kyoto.TFuncMap provides additional template functions, like dynamics.

Just give me a minute, I'll reproduce this issue on my local pc.

yznts commented 3 years ago

First of all, I see that you're using assets.Templates reference in template building.
If you're using sub-package, please, make sure that you're using embedding correctly. I don't know how paths are resolved in case of sub-packages.

Here is the most basic working setup with embed templates.

Screenshot 2021-10-19 at 10 16 57

Please note, that assets.go in my example is not in sub-package, but the part of a main app. Also, you need to use template file name while creating template instance without actual path.

aranw commented 3 years ago

Ah yeah I got it working now. My issue was I had template.New("templates/pages.home.html") and needed template.New("pages.home.html")

yznts commented 3 years ago

Awesome! Glad to see that it's working for you.
I'm closing this issue. In case of any issues feel free to open new issue, write to general chat or write to me directly.