render

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

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

Go to latest
Published: May 4, 2016 License: Apache-2.0 Imports: 9 Imported by: 22

README

##Render

GoDoc

Library for caching/executing templates to render HTTP responses.

API is similar to unrolled/render but cached/executed templates are each a collection of several files instead of all sharing the same set of files to allow for inheritance/embedding, similar to the approach suggested in http://stackoverflow.com/a/11468132.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// A set of functions that are available for templates to call during execution
	TextFuncs textTemplate.FuncMap
	HtmlFuncs htmlTemplate.FuncMap

	// The root directory to load templates from.
	Directory string

	// The character set to specify in HTTP responses. Defaults to UTF-8.
	Encoding string

	// If DisableCache is true, templates will be reloaded on every call.
	// By default, caching is enabled, so files are only loaded and parsed the first time.
	DisableCache bool
}

type Render

type Render struct {
	// contains filtered or unexported fields
}

Render maintains a template cache and exposes methods for rendering JSON or sets of templates to an HTTP response.

func New

func New(opts Options) *Render

New creates a new instance of Render with the given options, and an empty template cache.

func (*Render) GetHTMLTemplate

func (r *Render) GetHTMLTemplate(filenames ...string) (*htmlTemplate.Template, error)

GetHTMLTemplate returns an HTML template for the given set of filenames by loading it from cache if available, or loading and parsing the files from disk. Returns the template if found or an error if the template couldn't be loaded.

func (*Render) HTML

func (r *Render) HTML(out io.Writer, data interface{}, entryPoint string, files ...string) error

HTML loads the given set of template files and executes the template named entryPoint against the context data, writing the result to out. Returns error if the template could not be loaded or if executing the template failed.

func (*Render) StreamHTML

func (r *Render) StreamHTML(w http.ResponseWriter, status int, data interface{}, entryPoint string, files ...string) error

StreamHTML calls HTML() on its args and writes the output directly to the response. Does not buffer the executed template before rendering, so it can be used for writing really large responses without consuming memory. If executing the template fails, the status code is not changed; it will remain set to the provided value.

func (*Render) StreamText

func (r *Render) StreamText(w http.ResponseWriter, status int, data interface{}, entryPoint string, files ...string) error

StreamText calls Text() on its args and writes the output directly to the response with a text/plain Content-Type. It calls Text() so that it can accept go text templates rather than simply accepting text. Does not buffer the executed template before rendering, so it can be used for writing really large responses without consuming memory. If executing the template fails, the status code is not changed; it will remain set to the provided value.

func (*Render) Text

func (r *Render) Text(out io.Writer, data interface{}, entryPoint string, files ...string) error

Text loads the given set of template files and executes the template named entryPoint against the context data, writing the result to out. Returns error if the template could not be loaded or if executing the template failed.

func (*Render) WriteBinary

func (r *Render) WriteBinary(w http.ResponseWriter, status int, v []byte)

WriteBinary writes raw bytes as binary data to the reponse, with the given status code.

func (*Render) WriteHTML

func (r *Render) WriteHTML(w http.ResponseWriter, status int, data interface{}, entryPoint string, files ...string)

WriteHTML calls HTML() on its args and writes the output to the response with the given status. If the template can't be loaded or executed, the status is set to 500 and error details are written to the response body.

func (*Render) WriteJSON

func (r *Render) WriteJSON(w http.ResponseWriter, status int, data interface{})

WriteJSON marshals data to JSON and writes it to the response with the given status code. If marshaling fails, the status is set to 500 and error details are written to the response body.

Jump to

Keyboard shortcuts

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