render

package
v0.0.0-...-169a3af Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2015 License: MIT Imports: 9 Imported by: 5

Documentation

Overview

Package render provides html/templates rendering for the composable templates which can be called a set of templates.

// we define some layout/desktop.html
{{ template "header" .}}
{{ template "content" .}}

// then in the common/header.html, we define
{{ define "header" }} ... {{ end }}

// then we define content inside some index.html
{{ define "content" }}<p>Hello {{ .Hello }}</p>{{ end }}

// we can link those fragments as a render.TemplateSet
ts := render.TemplateSet("index", "desktop.html", "common/header.html", "index.html", "layout/desktop.html")

// New a renderer with some options
r := render.New(render.Options{
	Directory:     "templates",
	Funcs:         []template.FuncMap{someServer.DefaultRouteFuncs()},
	Delims:        render.Delims{"{{", "}}"},
	IndentJson:    true,
	UseBufPool:    true,
	IsDevelopment: false,
}, []*render.TemplateSet{ts})

// e.g. render some named templates
err := r.Html(w, http.StatusOK, "index", map[string]string{ "Hello": "World" })

// or we can render some json data
err := r.Json(w, http.StatusOK, "Hello World")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Delims

type Delims struct {
	Left  string
	Right string
}

Delims defines the deliminator for html/template.

type Options

type Options struct {
	// Templates lookup directory
	Directory string

	// User defined functions passed to the template engine
	Funcs []template.FuncMap

	// Deliminator for the html/template
	Delims Delims

	// Should indent json for readable format when call r.Json
	IndentJson bool

	// Should use the buf pool for the rendering or just use the bytes.Buffer
	UseBufPool bool

	// In development mode, the templates would be recompiled for each rendering call
	IsDevelopment bool
}

Options defines basic options for the renderer.

type Render

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

Render defines basic renderer data.

func New

func New(opt Options, tSets []*TemplateSet) *Render

New a renderer with given template sets and options.

func (*Render) Html

func (r *Render) Html(w http.ResponseWriter, status int, name string, binding interface{}) error

Html renders a named template set with given data binding, and writes the result and status to http.ResponseWriter

func (*Render) Json

func (r *Render) Json(w http.ResponseWriter, status int, v interface{}) error

Json renders a object and writes the result and status to http.ResponseWriter

type TemplateSet

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

TemplateSet defines a template set for composable template fragments

func NewTemplateSet

func NewTemplateSet(name string, entry string, tFile string, otherFiles ...string) *TemplateSet

NewTemplateSet returns a new template set, name provides the name for reverse searching, entry defines the most top template name, like the "layout.html"; have to at least give a template file or multiple template files used in this set.

Jump to

Keyboard shortcuts

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