func (r *Render) JSONP(w http.ResponseWriter, status int, callback string, v interface{}) error {
	head := Head{
		ContentType: ContentJSONP + r.compiledCharset,
		Status:      status,
	}
	j := JSONP{
		Head:     head,
		Indent:   r.opt.IndentJSON,
		Callback: callback,
	}
	return r.Render(w, j, v)
}
JSONP marshals the given interface object and writes the JSON response.
package render
import (
	"bytes"
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)
const (
	ContentBinary = "application/octet-stream"
	ContentHTML = "text/html"
	ContentJSON = "application/json"
	ContentJSONP = "application/javascript"
	ContentLength = "Content-Length"
	ContentText = "text/plain"
	ContentType = "Content-Type"
	ContentXHTML = "application/xhtml+xml"
	ContentXML = "text/xml"
	defaultCharset = "UTF-8"
)
type Delims struct {
	Left string
	Right string
}
type Options struct {
	Directory string
	Asset func(name string) ([]byte, error)
	AssetNames func() []string
	Layout string
	Extensions []string
	Funcs []template.FuncMap
	Delims Delims
	Charset string
	IndentJSON bool
	IndentXML bool
	PrefixJSON []byte
	PrefixXML []byte
	HTMLContentType string
	IsDevelopment bool
	UnEscapeHTML bool
	StreamingJSON bool
	RequirePartials bool
	RequireBlocks bool
	DisableHTTPErrorRendering bool
}
type HTMLOptions struct {
	Layout string
}
type Render struct {
	opt             Options
	templates       *template.Template
	compiledCharset string
}
func New(options ...Options) *Render {
	var o Options
	if len(options) == 0 {
		o = Options{}
	} else {
		o = options[0]
	}
	r := Render{
		opt: o,
	}
	r.prepareOptions()
	r.compileTemplates()
	if bufPool == nil {
		bufPool = NewBufferPool(64)
	}
	return &r
}
func (r *Render) prepareOptions() {
	if len(r.opt.Charset) == 0 {
		r.opt.Charset = defaultCharset
	}
	r.compiledCharset = "; charset=" + r.opt.Charset
	if len(r.opt.Directory) == 0 {
		r.opt.Directory = "templates"
	}
	if len(r.opt.Extensions) == 0 {
		r.opt.Extensions = []string{".tmpl"}
	}
	if len(r.opt.HTMLContentType) == 0 {
		r.opt.HTMLContentType = ContentHTML
	}
}
func (r *Render) compileTemplates() {
	if r.opt.Asset == nil || r.opt.AssetNames == nil {
		r.compileTemplatesFromDir()
		return
	}
	r.compileTemplatesFromAsset()
}
func (r *Render) compileTemplatesFromDir() {
	dir := r.opt.Directory
	r.templates = template.New(dir)
	r.templates.Delims(r.opt.Delims.Left, r.opt.Delims.Right)
	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if info == nil || info.IsDir() {
			return nil
		}
		rel, err := filepath.Rel(dir, path)
		if err != nil {
			return err
		}
		ext := ""
		if strings.Index(rel, ".") != -1 {
			ext = filepath.Ext(rel)
		}
		for _, extension := range r.opt.Extensions {
			if ext == extension {
				buf, err := ioutil.ReadFile(path)
				if err != nil {
					panic(err)
				}
				name := (rel[0 : len(rel)-len(ext)])
				tmpl := r.templates.New(filepath.ToSlash(name))
				for _, funcs := range r.opt.Funcs {
					tmpl.Funcs(funcs)
				}
				template.Must(tmpl.Funcs(helperFuncs).Parse(string(buf)))
				break
			}
		}
		return nil
	})
}
func (r *Render) compileTemplatesFromAsset() {
	dir := r.opt.Directory
	r.templates = template.New(dir)
	r.templates.Delims(r.opt.Delims.Left, r.opt.Delims.Right)
	for _, path := range r.opt.AssetNames() {
		if !strings.HasPrefix(path, dir) {
			continue
		}
		rel, err := filepath.Rel(dir, path)
		if err != nil {
			panic(err)
		}
		ext := ""
		if strings.Index(rel, ".") != -1 {
			ext = "." + strings.Join(strings.Split(rel, ".")[1:], ".")
		}
		for _, extension := range r.opt.Extensions {
			if ext == extension {
				buf, err := r.opt.Asset(path)
				if err != nil {
					panic(err)
				}
				name := (rel[0 : len(rel)-len(ext)])
				tmpl := r.templates.New(filepath.ToSlash(name))
				for _, funcs := range r.opt.Funcs {
					tmpl.Funcs(funcs)
				}
				template.Must(tmpl.Funcs(helperFuncs).Parse(string(buf)))
				break
			}
		}
	}
}
func (r *Render) TemplateLookup(t string) *template.Template {
	return r.templates.Lookup(t)
}
func (r *Render) execute(name string, binding interface{}) (*bytes.Buffer, error) {
	buf := new(bytes.Buffer)
	return buf, r.templates.ExecuteTemplate(buf, name, binding)
}
func (r *Render) addLayoutFuncs(name string, binding interface{}) {
	funcs := template.FuncMap{
		"yield": func() (template.HTML, error) {
			buf, err := r.execute(name, binding)
			return template.HTML(buf.String()), err
		},
		"current": func() (string, error) {
			return name, nil
		},
		"block": func(partialName string) (template.HTML, error) {
			log.Print("Render's `block` implementation is now depericated. Use `partial` as a drop in replacement.")
			fullPartialName := fmt.Sprintf("%s-%s", partialName, name)
			if r.opt.RequireBlocks || r.TemplateLookup(fullPartialName) != nil {
				buf, err := r.execute(fullPartialName, binding)
				return template.HTML(buf.String()), err
			}
			return "", nil
		},
		"partial": func(partialName string) (template.HTML, error) {
			fullPartialName := fmt.Sprintf("%s-%s", partialName, name)
			if r.opt.RequirePartials || r.TemplateLookup(fullPartialName) != nil {
				buf, err := r.execute(fullPartialName, binding)
				return template.HTML(buf.String()), err
			}
			return "", nil
		},
	}
	if tpl := r.templates.Lookup(name); tpl != nil {
		tpl.Funcs(funcs)
	}
}
func (r *Render) prepareHTMLOptions(htmlOpt []HTMLOptions) HTMLOptions {
	if len(htmlOpt) > 0 {
		return htmlOpt[0]
	}
	return HTMLOptions{
		Layout: r.opt.Layout,
	}
}
func (r *Render) Render(w http.ResponseWriter, e Engine, data interface{}) error {
	err := e.Render(w, data)
	if err != nil && !r.opt.DisableHTTPErrorRendering {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	return err
}
func (r *Render) Data(w http.ResponseWriter, status int, v []byte) error {
	head := Head{
		ContentType: ContentBinary,
		Status:      status,
	}
	d := Data{
		Head: head,
	}
	return r.Render(w, d, v)
}
func (r *Render) HTML(w http.ResponseWriter, status int, name string, binding interface{}, htmlOpt ...HTMLOptions) error {
	if r.opt.IsDevelopment {
		r.compileTemplates()
	}
	opt := r.prepareHTMLOptions(htmlOpt)
	if len(opt.Layout) > 0 {
		r.addLayoutFuncs(name, binding)
		name = opt.Layout
	}
	head := Head{
		ContentType: r.opt.HTMLContentType + r.compiledCharset,
		Status:      status,
	}
	h := HTML{
		Head:      head,
		Name:      name,
		Templates: r.templates,
	}
	return r.Render(w, h, binding)
}
func (r *Render) JSON(w http.ResponseWriter, status int, v interface{}) error {
	head := Head{
		ContentType: ContentJSON + r.compiledCharset,
		Status:      status,
	}
	j := JSON{
		Head:          head,
		Indent:        r.opt.IndentJSON,
		Prefix:        r.opt.PrefixJSON,
		UnEscapeHTML:  r.opt.UnEscapeHTML,
		StreamingJSON: r.opt.StreamingJSON,
	}
	return r.Render(w, j, v)
}