-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 with layout #441
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 |
After a little more thinking, I think I have a better idea for this feature. I would love to get anyone's thoughts. I would like to have a As long as you set up your layout templates correctly it would make it really easy to use, swap, and turn off layouts dynamically. A layout file would look like this: {{define "layouts/main--head}}
<html>
<body>
{{end}}
<!-- content gets inserted here -->
{{define "layouts/main--tail}}
</body>
</html> That's all it would take. Then in a handler: // use the main layout
func SomeHandler(c *fiber.Ctx) {
c.RenderWithLayout("some_template", "layouts/main", nil)
}
// use an alternative layout
func SomeHandler(c *fiber.Ctx) {
c.RenderWithLayout("some_template", "layouts/alt", nil)
}
// use no layout
func SomeHandler(c *fiber.Ctx) {
c.Render("some_template", nil)
} And here's the the basic gist of how it would be done (for brevity I'll leave out a bunch of code, this is just to communicate the idea) func (ctx *Ctx) RenderWithLayout(name string, layout string, bind interface{}) (err error) {
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
Templates.Render(buf, layout+"--head", bind)
Templates.Render(buf, name, bind)
Templates.Render(buf, layout+"--tail", bind)
c.SendBytes(buf.Bytes())
} |
@bdtomlin hi! 👋 Please, add more understandable title to this issue. |
Already in advance, in the next version the access to app with the context will be possible |
Sorry for the poor title initially, I should have waited to open this when I had clarified it more in my own mind. If everyone thinks this is a good feature, and is ok with the proposed API let me know. I would be glad to create a PR. |
UPDATE Please see my second comment for an improvement on this request as well as a concrete example. I have a workaround for this in my app, but I think it would be a great feature and it would have not cause any breaking changes.
--- below left for reference ---
Is your feature request related to a problem? No
Describe the solution you'd like
I would like to be able to access templates within my app so I can write a custom render function. Since app is unexported in type Ctx, it is not possible to access the templates with ctx.app.Settings.Templates.
One possible solution is to just export App. The only issue with that is it exposes a bunch of API that probably shouldn't be exposed.
Another solution is to add templates to Ctx instead of app.Settings. Honestly, templates are code and it doesn't seem like they really belong in settings. It really makes the most sense to me if they were added directly to app, but that does not fix the issue of access to them.
Describe alternatives you've considered
I've considered implementing my own template parser, but I would essentially be duplicating quite a bit of fiber code for a minor change so I would prefer to avoid it.
Additional context
The reason I'm writing a custom render function is so that I can have an api like this
The text was updated successfully, but these errors were encountered: