Skip to content

Commit 5519c27

Browse files
committed
Add parse test
1 parent 21c2f74 commit 5519c27

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

src/text/template/exampleparent_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func ExampleTemplate_parent() {
2929

3030
const index_html = `{{define "content"}}<h1>Welcome!</h1>{{end}}`
3131
index := template.Must(template.Must(base.Clone()).New("index.html").Parse(index_html))
32-
3332
{
3433
err := index.ExecuteTemplate(os.Stdout, "base.html", nil)
3534
if err != nil {
@@ -40,7 +39,6 @@ func ExampleTemplate_parent() {
4039
const about_html = `{{define "title"}}{{template parent .}} - About{{end}}
4140
{{define "content"}}<h1>About us</h1>{{end}}`
4241
about := template.Must(template.Must(base.Clone()).New("about.html").Parse(about_html))
43-
4442
{
4543
err := about.ExecuteTemplate(os.Stdout, "base.html", nil)
4644
if err != nil {

src/text/template/parse/node.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,11 @@ func (t *TemplateNode) String() string {
956956

957957
func (t *TemplateNode) writeTo(sb *strings.Builder) {
958958
sb.WriteString("{{template ")
959-
sb.WriteString(strconv.Quote(t.Name))
959+
if t.Parent {
960+
sb.WriteString("parent")
961+
} else {
962+
sb.WriteString(strconv.Quote(t.Name))
963+
}
960964
if t.Pipe != nil {
961965
sb.WriteByte(' ')
962966
t.Pipe.writeTo(sb)

src/text/template/parse/parse.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (t *Tree) ErrorContext(n Node) (location, context string) {
160160
// errorf formats the error and terminates processing.
161161
func (t *Tree) errorf(format string, args ...interface{}) {
162162
t.Root = nil
163-
format = fmt.Sprintf("\x1b[1;33mPAULDEV\x1b[0m template: %s:%d: %s", t.ParseName, t.token[0].line, format)
163+
format = fmt.Sprintf("template: %s:%d: %s", t.ParseName, t.token[0].line, format)
164164
panic(fmt.Errorf(format, args...))
165165
}
166166

@@ -580,7 +580,7 @@ func (t *Tree) blockControl() Node {
580580
block.add()
581581
block.stopParse()
582582

583-
return t.newTemplate(token.pos, token.line, name, false, pipe) // TODO: support parent?
583+
return t.newTemplate(token.pos, token.line, name, false, pipe)
584584
}
585585

586586
// Template:
@@ -595,16 +595,15 @@ func (t *Tree) templateControl() Node {
595595
parent bool
596596
)
597597
if token.typ == itemParent {
598-
// If we encounter the `parent` keyword, then we assume the current
599-
// definition we're in the midst of is an overriding of a previously
600-
// defined template, and the author's intent is to execute that
601-
// "parent" template as part of the "child".
598+
// If we encounter the `parent` keyword, then we assume the template we're
599+
// in the midst currently defining is overriding a previously defined
600+
// template, and that the template author's intent is to execute that
601+
// "parent" template as part of this "child" template.
602602
//
603-
// We know the name of the template currently being defined
604-
// (overridden), so we augment the name with a flag to indicate to the
605-
// execution step that it should lookup the "parent" template with that
606-
// name.
607-
//fmt.Printf("\x1b[1;31mTEMPLATE NAME: %q\x1b[0m\n", t.Name)
603+
// We know the name of the template currently being defined (i.e., the one
604+
// overriding a previous definition), so we set a flag to indicate to
605+
// template execution that it should lookup the "parent" template with the
606+
// same name.
608607
name = t.Name
609608
parent = true
610609
} else {

src/text/template/parse/parse_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ var parseTests = []parseTest{
236236
`{{template "x"}}`},
237237
{"template with arg", "{{template `x` .Y}}", noError,
238238
`{{template "x" .Y}}`},
239+
{"template with parent", "{{template parent .}}", noError,
240+
`{{template parent .}}`},
239241
{"with", "{{with .X}}hello{{end}}", noError,
240242
`{{with .X}}"hello"{{end}}`},
241243
{"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,

src/text/template/template.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ func (t *Template) lookup(name string, scope int) *Template {
209209
if scope >= len(t.tmpl[name]) {
210210
return nil
211211
}
212-
//fmt.Printf("\x1b[1;35mname: %q\tscope: %d\tlen: %d\x1b[0m\n", name, scope, len(t.tmpl[name]))
213212
return t.tmpl[name][scope] // TODO: invariant check: at least one
214213
}
215214

0 commit comments

Comments
 (0)