rendergold

package module
v0.0.0-...-c084600 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2014 License: MIT Imports: 8 Imported by: 0

README

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/martini-contrib/render"
	"github.com/yosssi/rendergold"
)

func main() {
	m := martini.Classic()
	m.Use(rendergold.Renderer()) // reads "templates" directory by default

	m.Get("/", func(r render.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/render"
	"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 render.Render) {
		r.HTML(
			http.StatusOK,
			"top",
			map[string]interface{}{
				"Title": "Gold - Template engine for Go",
			},
		)
	})
	m.Run()
}

Sample package using RenderGold

Docs

Documentation

Overview

Package rendergold provides a Martini middleware/handler for parsing Gold templates and rendering HTML.

package main

import (
  "github.com/go-martini/martini"
  "github.com/martini-contrib/render"
  "github.com/yosssi/rendergold"
)

func main() {
  m := martini.Classic()
  m.Use(rendergold.Renderer()) // reads "templates" directory by default

  m.Get("/", func(r render.Render) {
    r.HTML(200, "mytemplate", nil)
  })

  m.Run()
}

Index

Constants

View Source
const (

	// NameContentDelim is a delimiter for a combination of
	// a template name and its content.
	NameContentDelim = "@@@@"
)

Variables

This section is empty.

Functions

func Renderer

func Renderer(options ...Options) martini.Handler

Renderer is a Middleware that maps a render.Render service into the Martini handler chain. An single variadic rendergold.Options struct can be optionally provided to configure HTML rendering. The default directory for templates is "templates" and the default file extension is ".gold".

If MARTINI_ENV is set to "" or "development" then templates will be parsed every request. For more performance, set the MARTINI_ENV environment variable to "production"

Types

type Options

type Options struct {
	// Directory to load templates. Default is "templates"
	Directory string
	// Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Defaults to [].
	Func template.FuncMap
	// Appends the given charset to the Content-Type header. Default is "UTF-8".
	Charset string
	// Allows changing of output to XHTML instead of HTML. Default is "text/html"
	HTMLContentType string
	// Asset loads and returns the asset for the given name.
	Asset func(string) ([]byte, error)
}

Options is a struct for specifying configuration options for the render.Renderer middleware

type Render

type Render interface {
	// HTML renders a html template specified by the name and writes the result and given status to the http.ResponseWriter.
	HTML(status int, name string, v interface{}, htmlOpt ...render.HTMLOptions)
	// Redirect redirects the request.
	Redirect(location string, status ...int)
}

Render is a service that can be injected into a Martini handler. Render provides functions for easily writing HTML templates out to a http Response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL