package module
Version: v0.0.0-...-ab7c71c Latest Latest

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

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 10 Imported by: 0


Grender GoDoc Build Status

Deprecation notice

This package could be more focused, so it was split up into two improved packages:

Grender is a package that provides functionality for easily rendering HTML templates and JSON or XML data to a HTTP response. It is based on with some subtle modifications when it comes to rendering HTML templates.

  • Templates inheritance: {{/* extends "master.tmpl" */}}
  • Glob configuration: templates/*.tmpl
  • Normal templates as partials: {{ template "footer" .}}


Grender can be used with pretty much any web framework providing you can access the http.ResponseWriter from your handler. The rendering functions simply wraps Go's existing functionality for marshaling and rendering data.

  • HTML: Uses the html/template package to render HTML templates.
  • JSON: Uses the encoding/json package to marshal data into a JSON-encoded response.
  • XML: Uses the encoding/xml package to marshal data into an XML-encoded response.
  • Text: Passes the incoming string straight through to the http.ResponseWriter.
// main.go
package main

import (

func main() {
    r := grender.New(grender.Options{
        Charset: "ISO-8859-1",
        TemplatesGlob: "examples/*.tmpl",
    mux := http.NewServeMux()

    // This will set the Content-Type header to "application/json; charset=ISO-8859-1".
    mux.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) {
        r.JSON(w, http.StatusOK, map[string]string{"hello": "world"})

    // This will set the Content-Type header to "text/html; charset=ISO-8859-1".
    mux.HandleFunc("/html", func(w http.ResponseWriter, req *http.Request) {
        r.HTML(w, http.StatusOK, "hello.tmpl", "world")

    http.ListenAndServe("", mux)

Grender comes with a variety of configuration options. The defaults are listed below.

r := grender.New(grender.Options{
    Debug: false,       // If true, templates will be recompiled before each render call
    TemplatesGlob: "",  // Glob to your template files
    PartialsGlob: "",   // Glob to your patials or global templates
    Funcs: nil,         // Your template FuncMap
    Charset: "UTF-8",   // Charset to use for Content-Type header values
Extending another template

First, define your parent template like this.

file: master.tmpl

  {{template "content" .}}

Then, in a separate template file use a template comment on the first line to indicate that you want to extend the other template file.

file: child.tmpl

{{/* extends "master.tmpl" */}}

{{define "content"}}Hello world!{{end}}
More examples

The grender_test.go file contains additional usage examples.


See LICENSE file.




View Source
const (
	// ContentHTML HTTP header value for HTML data
	ContentHTML = "text/html"

	// ContentJSON HTTP header value for JSON data
	ContentJSON = "application/json"

	// ContentType HTTP header name for defining the content type
	ContentType = "Content-Type"

	// ContentText header value for Text data.
	ContentText = "text/plain"

	// ContentXML header value for XML data.
	ContentXML = "text/xml"

	// DefaultCharset for when no specific Charset Options was given
	DefaultCharset = "UTF-8"


This section is empty.


This section is empty.


type BufferPool

type BufferPool struct {
	// contains filtered or unexported fields

BufferPool implements a pool of bytes.Buffers in the form of a bounded channel. Pulled from the package (Apache licensed).

func NewBufferPool

func NewBufferPool(size int) (bp *BufferPool)

NewBufferPool creates a new BufferPool bounded to the given size.

func (*BufferPool) Get

func (bp *BufferPool) Get() (b *bytes.Buffer)

Get gets a Buffer from the BufferPool, or creates a new one if none are available in the pool.

func (*BufferPool) Put

func (bp *BufferPool) Put(b *bytes.Buffer)

Put returns the given Buffer to the BufferPool.

type Grender

type Grender struct {
	Options   Options
	Templates templates

Grender provides functions for easily writing HTML templates & JSON out to a HTTP Response.

func New

func New(optsarg ...Options) *Grender

New creates a new Renderer with the given Options

func (*Grender) HTML

func (r *Grender) HTML(w http.ResponseWriter, statusCode int, templateName string, data interface{}) error

HTML executes the template and writes to the responsewriter

func (*Grender) JSON

func (r *Grender) JSON(w http.ResponseWriter, statusCode int, data interface{}) error

JSON renders the data as a JSON HTTP response to the ResponseWriter

func (*Grender) Text

func (r *Grender) Text(w http.ResponseWriter, statusCode int, data string) error

Text writes the data as a JSON HTTP response to the ResponseWriter

func (*Grender) XML

func (r *Grender) XML(w http.ResponseWriter, statusCode int, data interface{}) error

XML writes the data as a XML HTTP response to the ResponseWriter

type Options

type Options struct {
	// With Debug set to true, templates will be recompiled before every render call.
	Debug bool

	// The glob string to your templates
	TemplatesGlob string

	// The Glob string for additional templates
	PartialsGlob string

	// The function map to pass to each HTML template
	Funcs template.FuncMap

	// Charset for responses
	Charset string

Options holds the configuration Options for a Renderer

Jump to

Keyboard shortcuts

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