2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2011-12-07 02:11:29 +01:00
|
|
|
"io"
|
2011-12-13 20:16:27 +01:00
|
|
|
"io/ioutil"
|
2011-09-16 17:47:21 +02:00
|
|
|
"path/filepath"
|
2011-12-13 20:16:27 +01:00
|
|
|
"sync"
|
2011-12-07 02:11:29 +01:00
|
|
|
"text/template"
|
2011-12-13 20:16:27 +01:00
|
|
|
"text/template/parse"
|
2011-09-16 17:47:21 +02:00
|
|
|
)
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// Template is a specialized Template from "text/template" that produces a safe
|
|
|
|
// HTML document fragment.
|
2011-12-07 02:11:29 +01:00
|
|
|
type Template struct {
|
|
|
|
escaped bool
|
2011-12-13 20:16:27 +01:00
|
|
|
// We could embed the text/template field, but it's safer not to because
|
|
|
|
// we need to keep our version of the name space and the underlying
|
|
|
|
// template's in sync.
|
2013-11-06 20:49:01 +01:00
|
|
|
text *template.Template
|
|
|
|
// The underlying template's parse tree, updated to be HTML-safe.
|
|
|
|
Tree *parse.Tree
|
2011-12-13 20:16:27 +01:00
|
|
|
*nameSpace // common to all associated templates
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// nameSpace is the data structure shared by all templates in an association.
|
|
|
|
type nameSpace struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
set map[string]*Template
|
|
|
|
}
|
|
|
|
|
2012-03-31 00:09:55 +02:00
|
|
|
// Templates returns a slice of the templates associated with t, including t
|
|
|
|
// itself.
|
|
|
|
func (t *Template) Templates() []*Template {
|
|
|
|
ns := t.nameSpace
|
|
|
|
ns.mu.Lock()
|
|
|
|
defer ns.mu.Unlock()
|
|
|
|
// Return a slice so we don't expose the map.
|
|
|
|
m := make([]*Template, 0, len(ns.set))
|
|
|
|
for _, v := range ns.set {
|
|
|
|
m = append(m, v)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// escape escapes all associated templates.
|
|
|
|
func (t *Template) escape() error {
|
2011-12-13 20:16:27 +01:00
|
|
|
t.nameSpace.mu.Lock()
|
2013-07-16 08:54:42 +02:00
|
|
|
defer t.nameSpace.mu.Unlock()
|
2011-12-13 20:16:27 +01:00
|
|
|
if !t.escaped {
|
2013-07-16 08:54:42 +02:00
|
|
|
if err := escapeTemplates(t, t.Name()); err != nil {
|
|
|
|
return err
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
t.escaped = true
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute applies a parsed template to the specified data object,
|
|
|
|
// writing the output to wr.
|
2014-07-19 10:53:52 +02:00
|
|
|
// If an error occurs executing the template or writing its output,
|
|
|
|
// execution stops, but partial results may already have been written to
|
|
|
|
// the output writer.
|
|
|
|
// A template may be executed safely in parallel.
|
2013-07-16 08:54:42 +02:00
|
|
|
func (t *Template) Execute(wr io.Writer, data interface{}) error {
|
|
|
|
if err := t.escape(); err != nil {
|
|
|
|
return err
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
|
|
|
return t.text.Execute(wr, data)
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// ExecuteTemplate applies the template associated with t that has the given
|
|
|
|
// name to the specified data object and writes the output to wr.
|
2014-07-19 10:53:52 +02:00
|
|
|
// If an error occurs executing the template or writing its output,
|
|
|
|
// execution stops, but partial results may already have been written to
|
|
|
|
// the output writer.
|
|
|
|
// A template may be executed safely in parallel.
|
2012-01-12 02:31:45 +01:00
|
|
|
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
|
2012-03-02 21:01:37 +01:00
|
|
|
tmpl, err := t.lookupAndEscapeTemplate(name)
|
2012-01-12 02:31:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tmpl.text.Execute(wr, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupAndEscapeTemplate guarantees that the template with the given name
|
|
|
|
// is escaped, or returns an error if it cannot be. It returns the named
|
|
|
|
// template.
|
2012-03-02 21:01:37 +01:00
|
|
|
func (t *Template) lookupAndEscapeTemplate(name string) (tmpl *Template, err error) {
|
2011-12-13 20:16:27 +01:00
|
|
|
t.nameSpace.mu.Lock()
|
2012-01-12 02:31:45 +01:00
|
|
|
defer t.nameSpace.mu.Unlock()
|
|
|
|
tmpl = t.set[name]
|
2012-03-31 00:09:55 +02:00
|
|
|
if tmpl == nil {
|
|
|
|
return nil, fmt.Errorf("html/template: %q is undefined", name)
|
|
|
|
}
|
|
|
|
if tmpl.text.Tree == nil || tmpl.text.Root == nil {
|
|
|
|
return nil, fmt.Errorf("html/template: %q is an incomplete template", name)
|
|
|
|
}
|
|
|
|
if t.text.Lookup(name) == nil {
|
2011-12-14 16:41:54 +01:00
|
|
|
panic("html/template internal error: template escaping out of sync")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2011-12-14 16:41:54 +01:00
|
|
|
if tmpl != nil && !tmpl.escaped {
|
2011-12-13 20:16:27 +01:00
|
|
|
err = escapeTemplates(tmpl, name)
|
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
return tmpl, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Parse parses a string into a template. Nested template definitions
|
|
|
|
// will be associated with the top-level template t. Parse may be
|
|
|
|
// called multiple times to parse definitions of templates to associate
|
|
|
|
// with t. It is an error if a resulting template is non-empty (contains
|
|
|
|
// content other than template definitions) and would replace a
|
|
|
|
// non-empty template with the same name. (In multiple calls to Parse
|
|
|
|
// with the same receiver template, only one call can contain text
|
|
|
|
// other than space, comments, and template definitions.)
|
|
|
|
func (t *Template) Parse(src string) (*Template, error) {
|
|
|
|
t.nameSpace.mu.Lock()
|
|
|
|
t.escaped = false
|
|
|
|
t.nameSpace.mu.Unlock()
|
|
|
|
ret, err := t.text.Parse(src)
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
// In general, all the named templates might have changed underfoot.
|
|
|
|
// Regardless, some new ones may have been defined.
|
|
|
|
// The template.Template set has been updated; update ours.
|
|
|
|
t.nameSpace.mu.Lock()
|
|
|
|
defer t.nameSpace.mu.Unlock()
|
|
|
|
for _, v := range ret.Templates() {
|
|
|
|
name := v.Name()
|
|
|
|
tmpl := t.set[name]
|
|
|
|
if tmpl == nil {
|
|
|
|
tmpl = t.new(name)
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
// Restore our record of this text/template to its unescaped original state.
|
2011-12-13 20:16:27 +01:00
|
|
|
tmpl.escaped = false
|
|
|
|
tmpl.text = v
|
2013-11-06 20:49:01 +01:00
|
|
|
tmpl.Tree = v.Tree
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// AddParseTree creates a new template with the name and parse tree
|
|
|
|
// and associates it with t.
|
|
|
|
//
|
|
|
|
// It returns an error if t has already been executed.
|
|
|
|
func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error) {
|
|
|
|
t.nameSpace.mu.Lock()
|
|
|
|
defer t.nameSpace.mu.Unlock()
|
|
|
|
if t.escaped {
|
|
|
|
return nil, fmt.Errorf("html/template: cannot AddParseTree to %q after it has executed", t.Name())
|
|
|
|
}
|
|
|
|
text, err := t.text.AddParseTree(name, tree)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &Template{
|
|
|
|
false,
|
|
|
|
text,
|
2013-11-06 20:49:01 +01:00
|
|
|
text.Tree,
|
2012-03-02 21:01:37 +01:00
|
|
|
t.nameSpace,
|
|
|
|
}
|
|
|
|
t.set[name] = ret
|
|
|
|
return ret, nil
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// Clone returns a duplicate of the template, including all associated
|
|
|
|
// templates. The actual representation is not copied, but the name space of
|
|
|
|
// associated templates is, so further calls to Parse in the copy will add
|
|
|
|
// templates to the copy but not to the original. Clone can be used to prepare
|
|
|
|
// common templates and use them with variant definitions for other templates
|
|
|
|
// by adding the variants after the clone is made.
|
|
|
|
//
|
|
|
|
// It returns an error if t has already been executed.
|
|
|
|
func (t *Template) Clone() (*Template, error) {
|
|
|
|
t.nameSpace.mu.Lock()
|
|
|
|
defer t.nameSpace.mu.Unlock()
|
|
|
|
if t.escaped {
|
|
|
|
return nil, fmt.Errorf("html/template: cannot Clone %q after it has executed", t.Name())
|
|
|
|
}
|
|
|
|
textClone, err := t.text.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &Template{
|
|
|
|
false,
|
|
|
|
textClone,
|
2013-11-06 20:49:01 +01:00
|
|
|
textClone.Tree,
|
2012-03-02 21:01:37 +01:00
|
|
|
&nameSpace{
|
|
|
|
set: make(map[string]*Template),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, x := range textClone.Templates() {
|
|
|
|
name := x.Name()
|
|
|
|
src := t.set[name]
|
|
|
|
if src == nil || src.escaped {
|
|
|
|
return nil, fmt.Errorf("html/template: cannot Clone %q after it has executed", t.Name())
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
x.Tree = x.Tree.Copy()
|
2012-03-02 21:01:37 +01:00
|
|
|
ret.set[name] = &Template{
|
|
|
|
false,
|
|
|
|
x,
|
2013-11-06 20:49:01 +01:00
|
|
|
x.Tree,
|
2012-03-02 21:01:37 +01:00
|
|
|
ret.nameSpace,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:11:29 +01:00
|
|
|
// New allocates a new HTML template with the given name.
|
|
|
|
func New(name string) *Template {
|
2011-12-13 20:16:27 +01:00
|
|
|
tmpl := &Template{
|
|
|
|
false,
|
|
|
|
template.New(name),
|
2013-11-06 20:49:01 +01:00
|
|
|
nil,
|
2011-12-13 20:16:27 +01:00
|
|
|
&nameSpace{
|
|
|
|
set: make(map[string]*Template),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tmpl.set[name] = tmpl
|
|
|
|
return tmpl
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// New allocates a new HTML template associated with the given one
|
|
|
|
// and with the same delimiters. The association, which is transitive,
|
|
|
|
// allows one template to invoke another with a {{template}} action.
|
|
|
|
func (t *Template) New(name string) *Template {
|
|
|
|
t.nameSpace.mu.Lock()
|
|
|
|
defer t.nameSpace.mu.Unlock()
|
|
|
|
return t.new(name)
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// new is the implementation of New, without the lock.
|
|
|
|
func (t *Template) new(name string) *Template {
|
|
|
|
tmpl := &Template{
|
|
|
|
false,
|
|
|
|
t.text.New(name),
|
2013-11-06 20:49:01 +01:00
|
|
|
nil,
|
2011-12-13 20:16:27 +01:00
|
|
|
t.nameSpace,
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
tmpl.set[name] = tmpl
|
|
|
|
return tmpl
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Name returns the name of the template.
|
|
|
|
func (t *Template) Name() string {
|
|
|
|
return t.text.Name()
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
// FuncMap is the type of the map defining the mapping from names to
|
|
|
|
// functions. Each function must have either a single return value, or two
|
|
|
|
// return values of which the second has type error. In that case, if the
|
|
|
|
// second (error) argument evaluates to non-nil during execution, execution
|
|
|
|
// terminates and Execute returns that error. FuncMap has the same base type
|
2013-01-29 21:52:43 +01:00
|
|
|
// as FuncMap in "text/template", copied here so clients need not import
|
|
|
|
// "text/template".
|
2012-01-12 02:31:45 +01:00
|
|
|
type FuncMap map[string]interface{}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Funcs adds the elements of the argument map to the template's function map.
|
|
|
|
// It panics if a value in the map is not a function with appropriate return
|
|
|
|
// type. However, it is legal to overwrite elements of the map. The return
|
|
|
|
// value is the template, so calls can be chained.
|
2012-01-12 02:31:45 +01:00
|
|
|
func (t *Template) Funcs(funcMap FuncMap) *Template {
|
|
|
|
t.text.Funcs(template.FuncMap(funcMap))
|
2011-12-13 20:16:27 +01:00
|
|
|
return t
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Delims sets the action delimiters to the specified strings, to be used in
|
|
|
|
// subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
|
|
|
|
// definitions will inherit the settings. An empty delimiter stands for the
|
|
|
|
// corresponding default: {{ or }}.
|
|
|
|
// The return value is the template, so calls can be chained.
|
|
|
|
func (t *Template) Delims(left, right string) *Template {
|
|
|
|
t.text.Delims(left, right)
|
|
|
|
return t
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Lookup returns the template with the given name that is associated with t,
|
|
|
|
// or nil if there is no such template.
|
|
|
|
func (t *Template) Lookup(name string) *Template {
|
|
|
|
t.nameSpace.mu.Lock()
|
|
|
|
defer t.nameSpace.mu.Unlock()
|
|
|
|
return t.set[name]
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// Must is a helper that wraps a call to a function returning (*Template, error)
|
|
|
|
// and panics if the error is non-nil. It is intended for use in variable initializations
|
|
|
|
// such as
|
|
|
|
// var t = template.Must(template.New("name").Parse("html"))
|
2011-12-13 20:16:27 +01:00
|
|
|
func Must(t *Template, err error) *Template {
|
2012-01-12 02:31:45 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
return t
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// ParseFiles creates a new Template and parses the template definitions from
|
|
|
|
// the named files. The returned template's name will have the (base) name and
|
|
|
|
// (parsed) contents of the first file. There must be at least one file.
|
|
|
|
// If an error occurs, parsing stops and the returned *Template is nil.
|
|
|
|
func ParseFiles(filenames ...string) (*Template, error) {
|
|
|
|
return parseFiles(nil, filenames...)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// ParseFiles parses the named files and associates the resulting templates with
|
|
|
|
// t. If an error occurs, parsing stops and the returned template is nil;
|
|
|
|
// otherwise it is t. There must be at least one file.
|
|
|
|
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
|
|
|
|
return parseFiles(t, filenames...)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// parseFiles is the helper for the method and function. If the argument
|
|
|
|
// template is nil, it is created from the first file.
|
|
|
|
func parseFiles(t *Template, filenames ...string) (*Template, error) {
|
|
|
|
if len(filenames) == 0 {
|
|
|
|
// Not really a problem, but be consistent.
|
2012-03-31 00:09:55 +02:00
|
|
|
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
|
2011-12-07 02:11:29 +01:00
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
for _, filename := range filenames {
|
|
|
|
b, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
name := filepath.Base(filename)
|
|
|
|
// First template becomes return value if not already defined,
|
|
|
|
// and we use that one for subsequent New calls to associate
|
|
|
|
// all the templates together. Also, if this file has the same name
|
|
|
|
// as t, this file becomes the contents of t, so
|
|
|
|
// t, err := New(name).Funcs(xxx).ParseFiles(name)
|
|
|
|
// works. Otherwise we create a new template associated with t.
|
|
|
|
var tmpl *Template
|
|
|
|
if t == nil {
|
|
|
|
t = New(name)
|
|
|
|
}
|
|
|
|
if name == t.Name() {
|
|
|
|
tmpl = t
|
|
|
|
} else {
|
|
|
|
tmpl = t.New(name)
|
|
|
|
}
|
|
|
|
_, err = tmpl.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseGlob creates a new Template and parses the template definitions from the
|
|
|
|
// files identified by the pattern, which must match at least one file. The
|
|
|
|
// returned template will have the (base) name and (parsed) contents of the
|
|
|
|
// first file matched by the pattern. ParseGlob is equivalent to calling
|
|
|
|
// ParseFiles with the list of files matched by the pattern.
|
|
|
|
func ParseGlob(pattern string) (*Template, error) {
|
|
|
|
return parseGlob(nil, pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseGlob parses the template definitions in the files identified by the
|
|
|
|
// pattern and associates the resulting templates with t. The pattern is
|
|
|
|
// processed by filepath.Glob and must match at least one file. ParseGlob is
|
|
|
|
// equivalent to calling t.ParseFiles with the list of files matched by the
|
|
|
|
// pattern.
|
|
|
|
func (t *Template) ParseGlob(pattern string) (*Template, error) {
|
|
|
|
return parseGlob(t, pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseGlob is the implementation of the function and method ParseGlob.
|
|
|
|
func parseGlob(t *Template, pattern string) (*Template, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
filenames, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
if len(filenames) == 0 {
|
2012-03-31 00:09:55 +02:00
|
|
|
return nil, fmt.Errorf("html/template: pattern matches no files: %#q", pattern)
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
|
|
|
return parseFiles(t, filenames...)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|