Commit 2f7f679c authored by Russ Cox's avatar Russ Cox

html/template, text/template: clarify template redefinition behavior

Make two important points clearer:

 - Giving a template definition containing
   nothing but spaces has no effect.
 - Giving a template definition containing
   non-spaces can only be done once per template.

Fixes #16912.
Fixes #16913.
Fixes #17360.

Change-Id: Ie3971b83ab148b7c8bb800fe4a21579566378e3e
Reviewed-on: https://go-review.googlesource.com/31459
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: 's avatarRob Pike <r@golang.org>
Reviewed-by: 's avatarAndrew Gerrand <adg@golang.org>
parent ae14472a
...@@ -150,19 +150,23 @@ func (t *Template) DefinedTemplates() string { ...@@ -150,19 +150,23 @@ func (t *Template) DefinedTemplates() string {
return t.text.DefinedTemplates() return t.text.DefinedTemplates()
} }
// Parse parses a string into a template. Nested template definitions // Parse parses text as a template body for t.
// will be associated with the top-level template t. Parse may be // Named template definitions ({{define ...}} or {{block ...}} statements) in text
// called multiple times to parse definitions of templates to associate // define additional templates associated with t and are removed from the
// with t. It is an error if a resulting template is non-empty (contains // definition of t itself.
// content other than template definitions) and would replace a //
// non-empty template with the same name. (In multiple calls to Parse // A template definition with a body containing only white space and comments
// with the same receiver template, only one call can contain text // is considered empty and is not recorded as the template's body.
// other than space, comments, and template definitions.) // Each template can be given a non-empty definition at most once.
func (t *Template) Parse(src string) (*Template, error) { // That is, Parse may be called multiple times to parse definitions of templates
// to associate with t, but at most one such call can include a non-empty body for
// t itself, and each named associated template can be given at most one
// non-empty definition.
func (t *Template) Parse(text string) (*Template, error) {
t.nameSpace.mu.Lock() t.nameSpace.mu.Lock()
t.escapeErr = nil t.escapeErr = nil
t.nameSpace.mu.Unlock() t.nameSpace.mu.Unlock()
ret, err := t.text.Parse(src) ret, err := t.text.Parse(text)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -181,9 +181,18 @@ func (t *Template) Lookup(name string) *Template { ...@@ -181,9 +181,18 @@ func (t *Template) Lookup(name string) *Template {
return t.tmpl[name] return t.tmpl[name]
} }
// Parse defines the template by parsing the text. Nested template definitions will be // Parse parses text as a template body for t.
// associated with the top-level template t. Parse may be called multiple times // Named template definitions ({{define ...}} or {{block ...}} statements) in text
// to parse definitions of templates to associate with t. // define additional templates associated with t and are removed from the
// definition of t itself.
//
// A template definition with a body containing only white space and comments
// is considered empty and is not recorded as the template's body.
// Each template can be given a non-empty definition at most once.
// That is, Parse may be called multiple times to parse definitions of templates
// to associate with t, but at most one such call can include a non-empty body for
// t itself, and each named associated template can be given at most one
// non-empty definition.
func (t *Template) Parse(text string) (*Template, error) { func (t *Template) Parse(text string) (*Template, error) {
t.init() t.init()
t.muFuncs.RLock() t.muFuncs.RLock()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment