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

🔥 RENDER template files from embedded resources #422

Closed
JohannesKaufmann opened this issue May 29, 2020 · 5 comments
Closed

🔥 RENDER template files from embedded resources #422

JohannesKaufmann opened this issue May 29, 2020 · 5 comments

Comments

@JohannesKaufmann
Copy link

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 #209 and #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 😢.

@welcome
Copy link

welcome bot commented May 29, 2020

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

@Fenny
Copy link
Member

Fenny commented May 29, 2020

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 👍

@Fenny
Copy link
Member

Fenny commented Jun 13, 2020

@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 👍

@Fenny
Copy link
Member

Fenny commented Jun 14, 2020

@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 Fenny closed this as completed Jun 14, 2020
@JohannesKaufmann
Copy link
Author

@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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants