add HTML rendering capabilities and helper functions

This commit is contained in:
2024-11-12 23:46:40 +01:00
parent e44f56635b
commit f9990d37c0
8 changed files with 488 additions and 5 deletions
+17 -1
View File
@@ -10,9 +10,14 @@ import (
func main() {
r := ron.New()
htmlRender := ron.HTMLRender()
r.Renderer = htmlRender
r.GET("/", helloWorld)
r.GET("/json", helloWorldJSON)
r.POST("/another", anotherHelloWorld)
r.GET("/html", helloWorldHTML)
r.GET("/component", componentHTML)
slog.Info("Server is running at http://localhost:8080")
http.ListenAndServe(":8080", r)
@@ -27,5 +32,16 @@ func anotherHelloWorld(c *ron.Context) {
}
func helloWorldJSON(c *ron.Context) {
c.JSON(200, ron.D{"message": "hello world"})
c.JSON(200, ron.Data{"message": "hello world"})
}
func helloWorldHTML(c *ron.Context) {
c.HTML(200, "page.index.gohtml", ron.Data{
"title": "hello world",
"message": "hello world from html",
})
}
func componentHTML(c *ron.Context) {
c.HTML(200, "component.list.gohtml", ron.Data{})
}
@@ -0,0 +1,4 @@
<ul>
<li>elem1</li>
<li>elem2</li>
</ul>
+16
View File
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Data.title }}</title>
</head>
<body>
{{ .Data.message }}
</body>
</html>