render

package
v0.0.0-...-156df2f Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2013 License: MIT Imports: 7 Imported by: 0

README

render

Martini middleware/handler for easily rendering serialized JSON and HTML template responses.

API Reference

Usage

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

// main.go
package main

import (
  "github.com/codegangsta/martini"
  "github.com/codegangsta/martini-contrib/render"
)

func main() {
  m := martini.Classic()
  // render html templates from templates directory
  m.Use(render.Renderer("templates")) 

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

  m.Run()
}

<!-- templates/hello.tmpl -->
<h2>Hello {{.}}!</h2>

Authors

Documentation

Overview

Package render is a middleware for Martini that provides easy JSON serialization and HTML template rendering.

package main

import (
  "github.com/codegangsta/martini"
  "github.com/codegangsta/martini-contrib/render"
)

func main() {
  m := martini.Classic()
  m.Use(render.Renderer("templates"))

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

  m.Get("/json", func(r render.Render) {
    r.JSON(200, "hello world")
  })

  m.Run()
}

Index

Constants

View Source
const (
	ContentType = "Content-Type"
	ContentJSON = "application/json"
	ContentHTML = "text/html"
)

Variables

This section is empty.

Functions

func Renderer

func Renderer(dir string) martini.Handler

Renderer is a Middleware that maps a render.Render service into the Martini handler chain. Renderer will compile templates globbed in the given dir. Templates must have the .tmpl extension to be compiled.

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

Types

type Render

type Render interface {
	// JSON writes the given status and JSON serialized version of the given value to the http.ResponseWriter.
	JSON(status int, v 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{})
}

Render is a service that can be injected into a Martini handler. Render provides functions for easily writing JSON and 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