Fiber: 馃敟 RENDER template files from embedded resources

Created on 29 May 2020  路  5Comments  路  Source: gofiber/fiber

Is your feature request related to a problem?
I want to render templates on the server, using embedded files from pkger.

Describe the solution you'd like
It would be great if one could pass in a http.FileSystem instead of the string.

// right now
app.Settings.Templates = pug.New("./public/views", ".pug")


// alternative? 
app.Settings.Templates = pug.NewEmbed(pkger.Dir("/public/views"), ".pug")

// alternative??
app.Settings.Templates.Root = pkger.Dir("/public/views")

Additional context

I am already using embed to serve static files (like css) and it works great! 馃コ

similar to https://github.com/gofiber/fiber/issues/209 and https://github.com/gofiber/fiber/issues/375


_P.S.: I would mention the embed middleware in the docs (with the tag "FileSystem" so that you can find it in the search) and mention it in the static-files section._

_I was kind of disappointed - at first - that you didn't support that. Many routers don't have good support of embed, if at all. For example, echo has marked it as wont-fix 馃槩._

馃槑 Nice to Have 馃洜 In Progress

Most helpful comment

@Fenny great, that new _engine_ looks amazing.

This also solves some problems that I encountered in the meantime with the views. But this is now solved with Reload(true) 馃帀

P.S.: I'm currently evaluating different strategies for hot reloading html, css and js files for rendered websites. That makes the feedback loop alot faster and plays nicely with _air_. I will let you know in the coming days how the middleware (using lrserver) could look like. I'm also happy to do a PR then...

All 5 comments

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Hi @JohannesKaufmann, this should be possible.

<engine>.NewEmbed is good name, or maybe <engine>.Embed.

I will play with this today, I will keep you posted 馃憤

@JohannesKaufmann, we are currently adding http.FileSystem support.

engine := html.NewFileSystem(http.Dir("./views"), ".html")

Thanks to @arsmn we got a neat walk function for embedded file systems.

We are 50% done https://github.com/Fenny/template/blob/master/html/html_test.go#L103 and I will notify you when we tag a release 馃憤

@JohannesKaufmann if you update https://github.com/gofiber/template to the latest version, you can use the NewFileSystem method that allows you to pass in an http.FileServer interface.

pkger

https://github.com/markbates/pkger

package main

import (
    "github.com/gofiber/fiber"
    "github.com/gofiber/template/html"

    "github.com/markbates/pkger"
)

func main() {
    engine := html.NewFileSystem(pkger.Dir("/views"), ".html")

    app := fiber.New(&fiber.Settings{
        Views: engine,
    })

    // ...
}

packr

https://github.com/gobuffalo/packr

package main

import (
    "github.com/gofiber/fiber"
    "github.com/gofiber/template/html"

    "github.com/gobuffalo/packr/v2"
)

func main() {
    engine := html.NewFileSystem(packr.New("Templates", "/views"), ".html")

    app := fiber.New(&fiber.Settings{
        Views: engine,
    })

    // ...
}

go.rice

https://github.com/GeertJohan/go.rice

package main

import (
    "github.com/gofiber/fiber"
    "github.com/gofiber/template/html"

    "github.com/GeertJohan/go.rice"
)

func main() {
    engine := html.NewFileSystem(rice.MustFindBox("views").HTTPBox(), ".html")

    app := fiber.New(&fiber.Settings{
        Views: engine,
    })

    // ...
}

fileb0x

https://github.com/UnnoTed/fileb0x

package main

import (
    "github.com/gofiber/fiber"
    "github.com/gofiber/template/html"
    // your generated package
    "github.com/<user>/<repo>/static"
)

func main() {
    engine := html.NewFileSystem(static.HTTP, ".html")

    app := fiber.New(&fiber.Settings{
        Views: engine,
    })

    // ...
}

@Fenny great, that new _engine_ looks amazing.

This also solves some problems that I encountered in the meantime with the views. But this is now solved with Reload(true) 馃帀

P.S.: I'm currently evaluating different strategies for hot reloading html, css and js files for rendered websites. That makes the feedback loop alot faster and plays nicely with _air_. I will let you know in the coming days how the middleware (using lrserver) could look like. I'm also happy to do a PR then...

Was this page helpful?
0 / 5 - 0 ratings