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

HTML templating support #1515

Open
sparkacus opened this issue Feb 23, 2025 · 1 comment · May be fixed by #1533
Open

HTML templating support #1515

sparkacus opened this issue Feb 23, 2025 · 1 comment · May be fixed by #1533
Assignees

Comments

@sparkacus
Copy link

sparkacus commented Feb 23, 2025

Would you concider supporting HTML responses?

From what I understand, raw responses and static files are already an option, but opening up the ability to return HTML via handlers could widen the gofr ecosystem.

I for example use HTMX and Alpine JS. I have no desire to use what others call modern frameworks. HTMX keeps my frontend incredibly simple and allows me to render what I want server side, which for me is important.

HTMX+gofr could be a great use case here. gofr already supports static files, so the index.html can still be static so to speak and be served via the AddStaticFiles method, but HTMX attributes can be added to the index page to perform AJAX requests on load e.g.

<!doctype html>
<html>
  <head>
    <title>This is the title of the webpage!</title>
    <script src="https://unpkg.com/[email protected]" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
  </head>
  <body>
    <p>This is an example paragraph. Anything in the <strong>body</strong> tag will appear on the page, just like this <strong>p</strong> tag and its contents.</p>
    <div hx-get="/greet" hx-trigger="load"></div>
  </body>
</html>

By supporting HTML responses, it means we can keep the entire stack within the gofr ecosystem, thus benefiting from observability, which I think is the most critical use case.

Originally I've been using gin to serve the frontend and then routing to gofr, but it's disjointed.

At the moment I'm actually being naughty and abusing the deprecated UseMiddlewareWithContainer method in order to render HTML templates :) (NOTE: The example below doesn't include code that makes use of the container, but I am fetching data from the DB and/or another registered service)

package main

import (
	"html/template"
	"net/http"

	"gofr.dev/pkg/gofr"
	"gofr.dev/pkg/gofr/container"
)

// Define middleware with container access
func middleware(c *container.Container, handler http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Your custom logic here
		// For example, logging, authentication, etc.
		c.Debug(r)
		c.Debug(r.Method)
		c.Debug(r.URL)

		if r.Method == "GET" {
			switch r.URL.Path {
			case "/greet":
				tmpl := template.Must(template.ParseFiles("layout.html"))

				tmpl.Execute(w, nil)
				return
			}
		}

		// Continue with the request processing
		handler.ServeHTTP(w, r)
	})
}

func main() {
	// initialise gofr object
	a := gofr.New()

	a.UseMiddlewareWithContainer(middleware)
	a.Run()
}
@sparkacus
Copy link
Author

@vikash vikash self-assigned this Feb 27, 2025
@vikash vikash linked a pull request Feb 28, 2025 that will close this issue
@vikash vikash linked a pull request Feb 28, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants