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

[Enhancement]: Render custom strings #45

Open
3 tasks done
lennard-brinkhaus opened this issue Nov 24, 2022 · 2 comments
Open
3 tasks done

[Enhancement]: Render custom strings #45

lennard-brinkhaus opened this issue Nov 24, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@lennard-brinkhaus
Copy link

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:

  • I agree to follow Mojito's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests reports may be closed without explanation.
@lennard-brinkhaus lennard-brinkhaus added the enhancement New feature or request label Nov 24, 2022
@nilathedragon
Copy link
Contributor

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants