Skip to content

Martini middleware/handler for parsing Gold templates and rendering HTML

License

Notifications You must be signed in to change notification settings

yosssi/rendergold

Repository files navigation

RenderGold wercker status GoDoc

Martini middleware/handler for parsing Gold templates and rendering HTML

About Gold

Gold is a template engine for Go. Please visit its GitHub repository for more details.

Usage

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>

Options

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.
}))
// ...

Load templates from binary data generated by go-bindata

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()
}

Sample package using RenderGold

Docs

About

Martini middleware/handler for parsing Gold templates and rendering HTML

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages