go-mojito / mojito

Code your next Go web project with (a) Mojito! Mojito is a super-modular, fast, opinion-less framework to bootstrap your next Go web project.
https://go-mojito.infinytum.co
MIT License
24 stars 0 forks source link

[Enhancement]: Render custom strings #45

Open lennard-brinkhaus opened 1 year ago

lennard-brinkhaus commented 1 year ago

Description

It would be great, to have the possibility to render / show a custom HTML string and not only a path to a specific file.

Additional Context

No response

Code Snippet

package main

import "github.com/go-mojito/mojito"

func main() {
  mojito.GET("/", func(ctx mojito.RendererContext) {
    ctx.Render(`
      <html>
      <body>
      <h1>Hello World</h1>
      </body>
      </html>
`)
  })
  mojito.ListenAndServe(":8123")
}

Checklist:

nilathedragon commented 1 year ago

Thinking about how to design this. I foresee some non-string examples for this.

Let's say a PDF / Word document renderer wouldn't be happy with a string input so Render(tpl string) is not viable. I'm thinking either []byte or interface{} but I'd prefer not having interface parameters.

Would there be a situation that is not covered by []byte? Raw file contents and strings both work with that. @0SkillAllLuck

My current proposal would be Render(tpl []byte) error and the respective MustRender(tpl []byte) resulting in the proposed code snippet changing to:

package main

import "github.com/go-mojito/mojito"

func main() {
  mojito.GET("/", func(ctx mojito.RendererContext) {
    ctx.MustRender([]byte(`
      <html>
      <body>
      <h1>Hello World</h1>
      </body>
      </html>
    `))
  })
  mojito.ListenAndServe(":8123")
}
0SkillAllLuck commented 1 year ago

I agree, an alternative would be to have a seperate ctx.RenderString(tpl string) function but that would be the worse option I think. So going with your suggestion @nilathedragon looks like the better way to me.