Skip to content

Latest commit

 

History

History
119 lines (89 loc) · 3.58 KB

README.md

File metadata and controls

119 lines (89 loc) · 3.58 KB

renders

New template render of macaron framework

Macaron middleware/handler for rendering serialized JSON, XML, and HTML template responses.

Installation

go get github.com/go-macaron/renders

Examples

Check out the examples folder for some examples

Usage

render uses Go's html/template package to render html templates.

// main.go
package main

import (
	"gopkg.in/macaron.v1"

	"github.com/go-macaron/renders"
)

func main() {
	m := macaron.Classic()
	m.Use(renders.Renderer(
		renders.Options{
			Directory:  "templates",                // Specify what path to load the templates from.
			Extensions: []string{".tmpl", ".html"}, // Specify extensions to load for templates.
			//Funcs:           FuncMap,                    // Specify helper function maps for templates to access.
			Charset:         "UTF-8",     // Sets encoding for json and html content-types. Default is "UTF-8".
			IndentJSON:      true,        // Output human readable JSON
			IndentXML:       true,        // Output human readable XML
			HTMLContentType: "text/html", // Output XHTML content type instead of default "text/html"
		}))
	m.Get("/", func(r renders.Render) {
		r.HTML(200, "pages/index.html", map[string]interface{}{"Title": "Home"})
	})
	m.Get("/profile", func(r renders.Render) {
		r.HTML(200, "pages/profile.tmpl", map[string]interface{}{"Title": "Profile"})
	})

	m.Get("/map", func(r renders.Render) {
		r.HTML(200, "pages/map.html", map[string]interface{}{"Title": "Map"})
	})
	m.Run()
}

Options

renders.Renderer comes with a variety of configuration options:

// ...
m.Use(re