Vendor main dependencies.
This commit is contained in:
parent
49a09ab7dd
commit
dd5e3fba01
2738 changed files with 1045689 additions and 0 deletions
20
vendor/github.com/unrolled/render/LICENSE
generated
vendored
Normal file
20
vendor/github.com/unrolled/render/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Cory Jacobsen
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
41
vendor/github.com/unrolled/render/buffer.go
generated
vendored
Normal file
41
vendor/github.com/unrolled/render/buffer.go
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
package render
|
||||
|
||||
import "bytes"
|
||||
|
||||
// bufPool represents a reusable buffer pool for executing templates into.
|
||||
var bufPool *BufferPool
|
||||
|
||||
// BufferPool implements a pool of bytes.Buffers in the form of a bounded channel.
|
||||
// Pulled from the github.com/oxtoacart/bpool package (Apache licensed).
|
||||
type BufferPool struct {
|
||||
c chan *bytes.Buffer
|
||||
}
|
||||
|
||||
// NewBufferPool creates a new BufferPool bounded to the given size.
|
||||
func NewBufferPool(size int) (bp *BufferPool) {
|
||||
return &BufferPool{
|
||||
c: make(chan *bytes.Buffer, size),
|
||||
}
|
||||
}
|
||||
|
||||
// Get gets a Buffer from the BufferPool, or creates a new one if none are
|
||||
// available in the pool.
|
||||
func (bp *BufferPool) Get() (b *bytes.Buffer) {
|
||||
select {
|
||||
case b = <-bp.c:
|
||||
// reuse existing buffer
|
||||
default:
|
||||
// create new buffer
|
||||
b = bytes.NewBuffer([]byte{})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Put returns the given Buffer to the BufferPool.
|
||||
func (bp *BufferPool) Put(b *bytes.Buffer) {
|
||||
b.Reset()
|
||||
select {
|
||||
case bp.c <- b:
|
||||
default: // Discard the buffer if the pool is full.
|
||||
}
|
||||
}
|
55
vendor/github.com/unrolled/render/doc.go
generated
vendored
Normal file
55
vendor/github.com/unrolled/render/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*Package render is a package that provides functionality for easily rendering JSON, XML, binary data, and HTML templates.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
|
||||
"github.com/unrolled/render" // or "gopkg.in/unrolled/render.v1"
|
||||
)
|
||||
|
||||
type ExampleXml struct {
|
||||
XMLName xml.Name `xml:"example"`
|
||||
One string `xml:"one,attr"`
|
||||
Two string `xml:"two,attr"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := render.New()
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Write([]byte("Welcome, visit sub pages now."))
|
||||
})
|
||||
|
||||
mux.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) {
|
||||
r.Data(w, http.StatusOK, []byte("Some binary data here."))
|
||||
})
|
||||
|
||||
mux.HandleFunc("/text", func(w http.ResponseWriter, req *http.Request) {
|
||||
r.Text(w, http.StatusOK, "Plain text here")
|
||||
})
|
||||
|
||||
mux.HandleFunc("/json", func(w http.ResponseWriter, req *http.Request) {
|
||||
r.JSON(w, http.StatusOK, map[string]string{"hello": "json"})
|
||||
})
|
||||
|
||||
mux.HandleFunc("/jsonp", func(w http.ResponseWriter, req *http.Request) {
|
||||
r.JSONP(w, http.StatusOK, "callbackName", map[string]string{"hello": "jsonp"})
|
||||
})
|
||||
|
||||
mux.HandleFunc("/xml", func(w http.ResponseWriter, req *http.Request) {
|
||||
r.XML(w, http.StatusOK, ExampleXml{One: "hello", Two: "xml"})
|
||||
})
|
||||
|
||||
mux.HandleFunc("/html", func(w http.ResponseWriter, req *http.Request) {
|
||||
// Assumes you have a template in ./templates called "example.tmpl".
|
||||
// $ mkdir -p templates && echo "<h1>Hello HTML world.</h1>" > templates/example.tmpl
|
||||
r.HTML(w, http.StatusOK, "example", nil)
|
||||
})
|
||||
|
||||
http.ListenAndServe("0.0.0.0:3000", mux)
|
||||
}
|
||||
*/
|
||||
package render
|
202
vendor/github.com/unrolled/render/engine.go
generated
vendored
Normal file
202
vendor/github.com/unrolled/render/engine.go
generated
vendored
Normal file
|
@ -0,0 +1,202 @@
|
|||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Engine is the generic interface for all responses.
|
||||
type Engine interface {
|
||||
Render(http.ResponseWriter, interface{}) error
|
||||
}
|
||||
|
||||
// Head defines the basic ContentType and Status fields.
|
||||
type Head struct {
|
||||
ContentType string
|
||||
Status int
|
||||
}
|
||||
|
||||
// Data built-in renderer.
|
||||
type Data struct {
|
||||
Head
|
||||
}
|
||||
|
||||
// HTML built-in renderer.
|
||||
type HTML struct {
|
||||
Head
|
||||
Name string
|
||||
Templates *template.Template
|
||||
}
|
||||
|
||||
// JSON built-in renderer.
|
||||
type JSON struct {
|
||||
Head
|
||||
Indent bool
|
||||
UnEscapeHTML bool
|
||||
Prefix []byte
|
||||
StreamingJSON bool
|
||||
}
|
||||
|
||||
// JSONP built-in renderer.
|
||||
type JSONP struct {
|
||||
Head
|
||||
Indent bool
|
||||
Callback string
|
||||
}
|
||||
|
||||
// Text built-in renderer.
|
||||
type Text struct {
|
||||
Head
|
||||
}
|
||||
|
||||
// XML built-in renderer.
|
||||
type XML struct {
|
||||
Head
|
||||
Indent bool
|
||||
Prefix []byte
|
||||
}
|
||||
|
||||
// Write outputs the header content.
|
||||
func (h Head) Write(w http.ResponseWriter) {
|
||||
w.Header().Set(ContentType, h.ContentType)
|
||||
w.WriteHeader(h.Status)
|
||||
}
|
||||
|
||||
// Render a data response.
|
||||
func (d Data) Render(w http.ResponseWriter, v interface{}) error {
|
||||
c := w.Header().Get(ContentType)
|
||||
if c != "" {
|
||||
d.Head.ContentType = c
|
||||
}
|
||||
|
||||
d.Head.Write(w)
|
||||
w.Write(v.([]byte))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Render a HTML response.
|
||||
func (h HTML) Render(w http.ResponseWriter, binding interface{}) error {
|
||||
// Retrieve a buffer from the pool to write to.
|
||||
out := bufPool.Get()
|
||||
err := h.Templates.ExecuteTemplate(out, h.Name, binding)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
h.Head.Write(w)
|
||||
out.WriteTo(w)
|
||||
|
||||
// Return the buffer to the pool.
|
||||
bufPool.Put(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Render a JSON response.
|
||||
func (j JSON) Render(w http.ResponseWriter, v interface{}) error {
|
||||
if j.StreamingJSON {
|
||||
return j.renderStreamingJSON(w, v)
|
||||
}
|
||||
|
||||
var result []byte
|
||||
var err error
|
||||
|
||||
if j.Indent {
|
||||
result, err = json.MarshalIndent(v, "", " ")
|
||||
result = append(result, '\n')
|
||||
} else {
|
||||
result, err = json.Marshal(v)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Unescape HTML if needed.
|
||||
if j.UnEscapeHTML {
|
||||
result = bytes.Replace(result, []byte("\\u003c"), []byte("<"), -1)
|
||||
result = bytes.Replace(result, []byte("\\u003e"), []byte(">"), -1)
|
||||
result = bytes.Replace(result, []byte("\\u0026"), []byte("&"), -1)
|
||||
}
|
||||
|
||||
// JSON marshaled fine, write out the result.
|
||||
j.Head.Write(w)
|
||||
if len(j.Prefix) > 0 {
|
||||
w.Write(j.Prefix)
|
||||
}
|
||||
w.Write(result)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j JSON) renderStreamingJSON(w http.ResponseWriter, v interface{}) error {
|
||||
j.Head.Write(w)
|
||||
if len(j.Prefix) > 0 {
|
||||
w.Write(j.Prefix)
|
||||
}
|
||||
|
||||
return json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
// Render a JSONP response.
|
||||
func (j JSONP) Render(w http.ResponseWriter, v interface{}) error {
|
||||
var result []byte
|
||||
var err error
|
||||
|
||||
if j.Indent {
|
||||
result, err = json.MarshalIndent(v, "", " ")
|
||||
} else {
|
||||
result, err = json.Marshal(v)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// JSON marshaled fine, write out the result.
|
||||
j.Head.Write(w)
|
||||
w.Write([]byte(j.Callback + "("))
|
||||
w.Write(result)
|
||||
w.Write([]byte(");"))
|
||||
|
||||
// If indenting, append a new line.
|
||||
if j.Indent {
|
||||
w.Write([]byte("\n"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Render a text response.
|
||||
func (t Text) Render(w http.ResponseWriter, v interface{}) error {
|
||||
c := w.Header().Get(ContentType)
|
||||
if c != "" {
|
||||
t.Head.ContentType = c
|
||||
}
|
||||
|
||||
t.Head.Write(w)
|
||||
w.Write([]byte(v.(string)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Render an XML response.
|
||||
func (x XML) Render(w http.ResponseWriter, v interface{}) error {
|
||||
var result []byte
|
||||
var err error
|
||||
|
||||
if x.Indent {
|
||||
result, err = xml.MarshalIndent(v, "", " ")
|
||||
result = append(result, '\n')
|
||||
} else {
|
||||
result, err = xml.Marshal(v)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// XML marshaled fine, write out the result.
|
||||
x.Head.Write(w)
|
||||
if len(x.Prefix) > 0 {
|
||||
w.Write(x.Prefix)
|
||||
}
|
||||
w.Write(result)
|
||||
return nil
|
||||
}
|
21
vendor/github.com/unrolled/render/helpers.go
generated
vendored
Normal file
21
vendor/github.com/unrolled/render/helpers.go
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
// +build go1.6
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
// Included helper functions for use when rendering HTML.
|
||||
var helperFuncs = template.FuncMap{
|
||||
"yield": func() (string, error) {
|
||||
return "", fmt.Errorf("yield called with no layout defined")
|
||||
},
|
||||
"partial": func() (string, error) {
|
||||
return "", fmt.Errorf("block called with no layout defined")
|
||||
},
|
||||
"current": func() (string, error) {
|
||||
return "", nil
|
||||
},
|
||||
}
|
26
vendor/github.com/unrolled/render/helpers_pre16.go
generated
vendored
Normal file
26
vendor/github.com/unrolled/render/helpers_pre16.go
generated
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
// +build !go1.6
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
// Included helper functions for use when rendering HTML.
|
||||
var helperFuncs = template.FuncMap{
|
||||
"yield": func() (string, error) {
|
||||
return "", fmt.Errorf("yield called with no layout defined")
|
||||
},
|
||||
// `block` is deprecated! Use the `partial` below if you need this functionality still.
|
||||
// Otherwise, checkout Go's `block` implementation introduced in 1.6
|
||||
"block": func() (string, error) {
|
||||
return "", fmt.Errorf("block called with no layout defined")
|
||||
},
|
||||
"partial": func() (string, error) {
|
||||
return "", fmt.Errorf("block called with no layout defined")
|
||||
},
|
||||
"current": func() (string, error) {
|
||||
return "", nil
|
||||
},
|
||||
}
|
418
vendor/github.com/unrolled/render/render.go
generated
vendored
Normal file
418
vendor/github.com/unrolled/render/render.go
generated
vendored
Normal file
|
@ -0,0 +1,418 @@
|
|||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// ContentBinary header value for binary data.
|
||||
ContentBinary = "application/octet-stream"
|
||||
// ContentHTML header value for HTML data.
|
||||
ContentHTML = "text/html"
|
||||
// ContentJSON header value for JSON data.
|
||||
ContentJSON = "application/json"
|
||||
// ContentJSONP header value for JSONP data.
|
||||
ContentJSONP = "application/javascript"
|
||||
// ContentLength header constant.
|
||||
ContentLength = "Content-Length"
|
||||
// ContentText header value for Text data.
|
||||
ContentText = "text/plain"
|
||||
// ContentType header constant.
|
||||
ContentType = "Content-Type"
|
||||
// ContentXHTML header value for XHTML data.
|
||||
ContentXHTML = "application/xhtml+xml"
|
||||
// ContentXML header value for XML data.
|
||||
ContentXML = "text/xml"
|
||||
// Default character encoding.
|
||||
defaultCharset = "UTF-8"
|
||||
)
|
||||
|
||||
// helperFuncs had to be moved out. See helpers.go|helpers_pre16.go files.
|
||||
|
||||
// Delims represents a set of Left and Right delimiters for HTML template rendering.
|
||||
type Delims struct {
|
||||
// Left delimiter, defaults to {{.
|
||||
Left string
|
||||
// Right delimiter, defaults to }}.
|
||||
Right string
|
||||
}
|
||||
|
||||
// Options is a struct for specifying configuration options for the render.Render object.
|
||||
type Options struct {
|
||||
// Directory to load templates. Default is "templates".
|
||||
Directory string
|
||||
// Asset function to use in place of directory. Defaults to nil.
|
||||
Asset func(name string) ([]byte, error)
|
||||
// AssetNames function to use in place of directory. Defaults to nil.
|
||||
AssetNames func() []string
|
||||
// Layout template name. Will not render a layout if blank (""). Defaults to blank ("").
|
||||
Layout string
|
||||
// Extensions to parse template files from. Defaults to [".tmpl"].
|
||||
Extensions []string
|
||||
// Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Defaults to [].
|
||||
Funcs []template.FuncMap
|
||||
// Delims sets the action delimiters to the specified strings in the Delims struct.
|
||||
Delims Delims
|
||||
// Appends the given character set to the Content-Type header. Default is "UTF-8".
|
||||
Charset string
|
||||
// Outputs human readable JSON.
|
||||
IndentJSON bool
|
||||
// Outputs human readable XML. Default is false.
|
||||
IndentXML bool
|
||||
// Prefixes the JSON output with the given bytes. Default is false.
|
||||
PrefixJSON []byte
|
||||
// Prefixes the XML output with the given bytes.
|
||||
PrefixXML []byte
|
||||
// Allows changing of output to XHTML instead of HTML. Default is "text/html".
|
||||
HTMLContentType string
|
||||
// If IsDevelopment is set to true, this will recompile the templates on every request. Default is false.
|
||||
IsDevelopment bool
|
||||
// Unescape HTML characters "&<>" to their original values. Default is false.
|
||||
UnEscapeHTML bool
|
||||
// Streams JSON responses instead of marshalling prior to sending. Default is false.
|
||||
StreamingJSON bool
|
||||
// Require that all partials executed in the layout are implemented in all templates using the layout. Default is false.
|
||||
RequirePartials bool
|
||||
// Deprecated: Use the above `RequirePartials` instead of this. As of Go 1.6, blocks are built in. Default is false.
|
||||
RequireBlocks bool
|
||||
// Disables automatic rendering of http.StatusInternalServerError when an error occurs. Default is false.
|
||||
DisableHTTPErrorRendering bool
|
||||
}
|
||||
|
||||
// HTMLOptions is a struct for overriding some rendering Options for specific HTML call.
|
||||
type HTMLOptions struct {
|
||||
// Layout template name. Overrides Options.Layout.
|
||||
Layout string
|
||||
}
|
||||
|
||||
// Render is a service that provides functions for easily writing JSON, XML,
|
||||
// binary data, and HTML templates out to a HTTP Response.
|
||||
type Render struct {
|
||||
// Customize Secure with an Options struct.
|
||||
opt Options
|
||||
templates *template.Template
|
||||
compiledCharset string
|
||||
}
|
||||
|
||||
// New constructs a new Render instance with the supplied options.
|
||||
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()
|
||||
|
||||
// Create a new buffer pool for writing templates into.
|
||||
if bufPool == nil {
|
||||
bufPool = NewBufferPool(64)
|
||||
}
|
||||
|
||||
return &r
|
||||
}
|
||||
|
||||
func (r *Render) prepareOptions() {
|
||||
// Fill in the defaults if need be.
|
||||
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)
|
||||
|
||||
// Walk the supplied directory and compile any files that match our extension list.
|
||||
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||
// Fix same-extension-dirs bug: some dir might be named to: "users.tmpl", "local.html".
|
||||
// These dirs should be excluded as they are not valid golang templates, but files under
|
||||
// them should be treat as normal.
|
||||
// If is a dir, return immediately (dir is not a valid golang template).
|
||||
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))
|
||||
|
||||
// Add our funcmaps.
|
||||
for _, funcs := range r.opt.Funcs {
|
||||
tmpl.Funcs(funcs)
|
||||
}
|
||||
|
||||
// Break out if this parsing fails. We don't want any silent server starts.
|
||||
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))
|
||||
|
||||
// Add our funcmaps.
|
||||
for _, funcs := range r.opt.Funcs {
|
||||
tmpl.Funcs(funcs)
|
||||
}
|
||||
|
||||
// Break out if this parsing fails. We don't want any silent server starts.
|
||||
template.Must(tmpl.Funcs(helperFuncs).Parse(string(buf)))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TemplateLookup is a wrapper around template.Lookup and returns
|
||||
// the template with the given name that is associated with t, or nil
|
||||
// if there is no such template.
|
||||
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 safe HTML here since we are rendering our own template.
|
||||
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 safe HTML here since we are rendering our own template.
|
||||
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 safe HTML here since we are rendering our own template.
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
// Render is the generic function called by XML, JSON, Data, HTML, and can be called by custom implementations.
|
||||
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
|
||||
}
|
||||
|
||||
// Data writes out the raw bytes as binary data.
|
||||
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)
|
||||
}
|
||||
|
||||
// HTML builds up the response from the specified template and bindings.
|
||||
func (r *Render) HTML(w http.ResponseWriter, status int, name string, binding interface{}, htmlOpt ...HTMLOptions) error {
|
||||
// If we are in development mode, recompile the templates on every HTML request.
|
||||
if r.opt.IsDevelopment {
|
||||
r.compileTemplates()
|
||||
}
|
||||
|
||||
opt := r.prepareHTMLOptions(htmlOpt)
|
||||
// Assign a layout if there is one.
|
||||
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)
|
||||
}
|
||||
|
||||
// JSON marshals the given interface object and writes the JSON response.
|
||||
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)
|
||||
}
|
||||
|
||||
// JSONP marshals the given interface object and writes the JSON response.
|
||||
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)
|
||||
}
|
||||
|
||||
// Text writes out a string as plain text.
|
||||
func (r *Render) Text(w http.ResponseWriter, status int, v string) error {
|
||||
head := Head{
|
||||
ContentType: ContentText + r.compiledCharset,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
t := Text{
|
||||
Head: head,
|
||||
}
|
||||
|
||||
return r.Render(w, t, v)
|
||||
}
|
||||
|
||||
// XML marshals the given interface object and writes the XML response.
|
||||
func (r *Render) XML(w http.ResponseWriter, status int, v interface{}) error {
|
||||
head := Head{
|
||||
ContentType: ContentXML + r.compiledCharset,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
x := XML{
|
||||
Head: head,
|
||||
Indent: r.opt.IndentXML,
|
||||
Prefix: r.opt.PrefixXML,
|
||||
}
|
||||
|
||||
return r.Render(w, x, v)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue