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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃敟 Render with layout #441

Closed
bdtomlin opened this issue Jun 5, 2020 · 7 comments
Closed

馃敟 Render with layout #441

bdtomlin opened this issue Jun 5, 2020 · 7 comments

Comments

@bdtomlin
Copy link

bdtomlin commented Jun 5, 2020

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

render(c *fiber.Ctx, name string, layout string, bind interface{}){
  if layout == ""
    // write the template
    return
  end
  // write the layout head
  // write the template
  // write the layout tail
}
@welcome
Copy link

welcome bot commented Jun 5, 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

@bdtomlin
Copy link
Author

bdtomlin commented Jun 5, 2020

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 RenderWithLayout function. It would be similar to the Render function, but it would allow a great way to specify layouts.

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())
}

@koddr
Copy link
Contributor

koddr commented Jun 5, 2020

@bdtomlin hi! 馃憢

Please, add more understandable title to this issue.

@ReneWerner87
Copy link
Member

Already in advance, in the next version the access to app with the context will be possible
https://github.com/Fenny/fiber/blob/master/ctx.go#L162

@ReneWerner87 ReneWerner87 changed the title 馃殌 馃殌 Custom render function for layouts Jun 5, 2020
@bdtomlin bdtomlin changed the title 馃殌 Custom render function for layouts RenderWithLayout function Jun 5, 2020
@bdtomlin
Copy link
Author

bdtomlin commented Jun 5, 2020

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.

@Fenny
Copy link
Member

Fenny commented Jun 6, 2020

@bdtomlin, I would like to invite you to our Discord channel where it might be easier to chat about our template engine.

@Fenny Fenny changed the title RenderWithLayout function 馃敟 Render with layout Jun 12, 2020
@Fenny Fenny added this to To do in Development via automation Jun 12, 2020
@Fenny Fenny moved this from To do to In progress in Development Jun 12, 2020
@Fenny
Copy link
Member

Fenny commented Jun 12, 2020

Thank you @bdtomlin for your help!
https://github.com/gofiber/fiber/releases/tag/v1.11.1
https://github.com/gofiber/template

@Fenny Fenny closed this as completed Jun 12, 2020
Development automation moved this from In progress to Done Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development
  
Done
Development

No branches or pull requests

4 participants