Martini middleware/handler for parsing Gold templates and rendering HTML
Gold is a template engine for Go. Please visit its GitHub repository for more details.
Here is an example of server-side Go code:
package main
import (
"github.com/go-martini/martini"
"github.com/yosssi/rendergold"
)
func main() {
m := martini.Classic()
m.Use(rendergold.Renderer()) // reads "templates" directory by default
m.Get("/", func(r rendergold.Render) {
r.HTML(200, "top", nil) // parses "templates/top.gold"
})
m.Run()
}
Here is an example of templates/top.gold
:
doctype html
html
head
title RenderGold
body
h1 Hello RenderGold!
This template will be converted to the following HTML:
<!DOCTYPE html>
<html>
<head>
<title>RenderGold</title>
</head>
<body>
<h1>Hello RenderGold!</h1>
</body>
</html>
rendergold.Renderer
comes with a variety of configuration options:
// ...
m.Use(render.Renderer(render.Options{
Directory: "templates", // Specify what path to load the templates from. Default is "templates".
Func: template.FuncMap{AppHelpers}, // Specify helper function map for templates to access.
Charset: "UTF-8", // Sets encoding for html content-types. Default is "UTF-8".
HTMLContentType: "application/xhtml+xml", // Output XHTML content type instead of default "text/html".
Asset: Asset, // Asset loads and returns the asset for the given name.
}))
// ...
You can load templates from binary data generated by go-bindata by setting the Asset
function (which is generated by go-bindata) to the rendergold.renderer
via rendergold.Options
.
package main
import (
"net/http"
"github.com/martini-contrib/staticbin"
"github.com/yosssi/rendergold"
)
func main() {
m := staticbin.Classic(Asset)
// Set the `Asset` function generated by go-bindata
// to the `rendergold.renderer` via `rendergold.Options`.
m.Use(rendergold.Renderer(rendergold.Options{Asset: Asset}))
m.Get("/", func(r rendergold.Render) {
r.HTML(
http.StatusOK,
"top",
map[string]interface{}{
"Title": "Gold - Template engine for Go",
},
)
})
m.Run()
}