From b67b72da4337a16088c1780245a9fa1c45b4bca4 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Thu, 18 Aug 2011 10:38:08 +1000 Subject: [PATCH] doc/codelab: use new template package R=golang-dev, r CC=golang-dev https://golang.org/cl/4897048 --- doc/codelab/wiki/edit.html | 6 +-- doc/codelab/wiki/final-noclosure.go | 4 +- doc/codelab/wiki/final-noerror.go | 6 +-- doc/codelab/wiki/final.go | 5 ++- doc/codelab/wiki/htmlify.go | 4 +- doc/codelab/wiki/index.html | 68 +++++++++++++++-------------- doc/codelab/wiki/srcextract.go | 2 +- doc/codelab/wiki/view.html | 6 +-- doc/codelab/wiki/wiki.html | 49 +++++++++++---------- 9 files changed, 77 insertions(+), 73 deletions(-) diff --git a/doc/codelab/wiki/edit.html b/doc/codelab/wiki/edit.html index 7a5768ce96..c14953b17b 100644 --- a/doc/codelab/wiki/edit.html +++ b/doc/codelab/wiki/edit.html @@ -1,6 +1,6 @@ -

Editing {Title}

+

Editing {{.Title |html}}

-
-
+ +
diff --git a/doc/codelab/wiki/final-noclosure.go b/doc/codelab/wiki/final-noclosure.go index 2e1c3ec86c..067f502c6b 100644 --- a/doc/codelab/wiki/final-noclosure.go +++ b/doc/codelab/wiki/final-noclosure.go @@ -3,9 +3,9 @@ package main import ( "http" "io/ioutil" - "old/template" "os" "regexp" + "template" ) type Page struct { @@ -68,7 +68,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request) { } func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { - t, err := template.ParseFile(tmpl+".html", nil) + t, err := template.ParseFile(tmpl+".html") if err != nil { http.Error(w, err.String(), http.StatusInternalServerError) return diff --git a/doc/codelab/wiki/final-noerror.go b/doc/codelab/wiki/final-noerror.go index d9f5350de1..b8edbee9bd 100644 --- a/doc/codelab/wiki/final-noerror.go +++ b/doc/codelab/wiki/final-noerror.go @@ -3,8 +3,8 @@ package main import ( "http" "io/ioutil" - "old/template" "os" + "template" ) type Page struct { @@ -34,14 +34,14 @@ func editHandler(w http.ResponseWriter, r *http.Request) { if err != nil { p = &Page{Title: title} } - t, _ := template.ParseFile("edit.html", nil) + t, _ := template.ParseFile("edit.html") t.Execute(w, p) } func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) - t, _ := template.ParseFile("view.html", nil) + t, _ := template.ParseFile("view.html") t.Execute(w, p) } diff --git a/doc/codelab/wiki/final.go b/doc/codelab/wiki/final.go index 3d79d6e482..47a4c3473e 100644 --- a/doc/codelab/wiki/final.go +++ b/doc/codelab/wiki/final.go @@ -3,9 +3,9 @@ package main import ( "http" "io/ioutil" - "old/template" "os" "regexp" + "template" ) type Page struct { @@ -59,7 +59,8 @@ var templates = make(map[string]*template.Template) func init() { for _, tmpl := range []string{"edit", "view"} { - templates[tmpl] = template.MustParseFile(tmpl+".html", nil) + t := template.Must(template.ParseFile(tmpl+".html")) + templates[tmpl] = t } } diff --git a/doc/codelab/wiki/htmlify.go b/doc/codelab/wiki/htmlify.go index a89d6b7f34..9e7605b921 100644 --- a/doc/codelab/wiki/htmlify.go +++ b/doc/codelab/wiki/htmlify.go @@ -1,12 +1,12 @@ package main import ( - "old/template" + "template" "os" "io/ioutil" ) func main() { b, _ := ioutil.ReadAll(os.Stdin) - template.HTMLFormatter(os.Stdout, "", b) + template.HTMLEscape(os.Stdout, b) } diff --git a/doc/codelab/wiki/index.html b/doc/codelab/wiki/index.html index 103986a82b..50e9db5e99 100644 --- a/doc/codelab/wiki/index.html +++ b/doc/codelab/wiki/index.html @@ -7,7 +7,7 @@ Covered in this codelab: @@ -426,27 +426,27 @@ This function will work fine, but all that hard-coded HTML is ugly. Of course, there is a better way.

-

The old/template package

+

The template package

-The old/template package is part of the Go standard library. +The template package is part of the Go standard library. (A new template package is coming; this code lab will be updated soon.) We can -use old/template to keep the HTML in a separate file, allowing +use template to keep the HTML in a separate file, allowing us to change the layout of our edit page without modifying the underlying Go code.

-First, we must add old/template to the list of imports: +First, we must add template to the list of imports:

 import (
 	"http"
 	"io/ioutil"
-	"old/template"
 	"os"
+	"template"
 )
 
@@ -456,10 +456,10 @@ Open a new file named edit.html, and add the following lines:

-<h1>Editing {Title}</h1>
+<h1>Editing {{.Title |html}}</h1>
 
-<form action="/save/{Title}" method="POST">
-<div><textarea name="body" rows="20" cols="80">{Body|html}</textarea></div>
+<form action="/save/{{.Title |html}}" method="POST">
+<div><textarea name="body" rows="20" cols="80">{{printf "%s" .Body |html}}</textarea></div>
 <div><input type="submit" value="Save"></div>
 </form>
 
@@ -476,7 +476,7 @@ func editHandler(w http.ResponseWriter, r *http.Request) { if err != nil { p = &Page{Title: title} } - t, _ := template.ParseFile("edit.html", nil) + t, _ := template.ParseFile("edit.html") t.Execute(w, p) } @@ -487,19 +487,21 @@ The function template.ParseFile will read the contents of

-The method t.Execute replaces all occurrences of -{Title} and {Body} with the values of -p.Title and p.Body, and writes the resultant -HTML to the http.ResponseWriter. +The method t.Execute executes the template, writing the +generated HTML to the http.ResponseWriter. +The .Title and .Body dotted identifiers refer to +p.Title and p.Body.

-Note that we've used {Body|html} in the above template. -The |html part asks the template engine to pass the value -Body through the html formatter before outputting it, -which escapes HTML characters (such as replacing > with -&gt;). -This will prevent user data from corrupting the form HTML. +Template directives are enclosed in double curly braces. +The printf "%s" .Body instruction is a function call +that outputs .Body as a string instead of a stream of bytes, +the same as a call to fmt.Printf. +The |html part of each directive pipes the value through the +html formatter before outputting it, which escapes HTML +characters (such as replacing > with &gt;), +preventing user data from corrupting the form HTML.

@@ -513,11 +515,11 @@ While we're working with templates, let's create a template for our

-<h1>{Title}</h1>
+<h1>{{.Title |html}}</h1>
 
-<p>[<a href="/edit/{Title}">edit</a>]</p>
+<p>[<a href="/edit/{{.Title |html}}">edit</a>]</p>
 
-<div>{Body}</div>
+<div>{{printf "%s" .Body |html}}</div>
 

@@ -528,7 +530,7 @@ Modify viewHandler accordingly: func viewHandler(w http.ResponseWriter, r *http.Request) { title := r.URL.Path[lenPath:] p, _ := loadPage(title) - t, _ := template.ParseFile("view.html", nil) + t, _ := template.ParseFile("view.html") t.Execute(w, p) } @@ -708,16 +710,17 @@ var templates = make(map[string]*template.Template)

Then we create an init function, which will be called before main at program initialization. The function -template.MustParseFile is a convenience wrapper around -ParseFile that does not return an error code; instead, it panics -if an error is encountered. A panic is appropriate here; if the templates can't -be loaded the only sensible thing to do is exit the program. +template.Must is a convenience wrapper that panics when passed a +non-nil os.Error value, and otherwise returns the +*Template unaltered. A panic is appropriate here; if the templates +can't be loaded the only sensible thing to do is exit the program.

 func init() {
 	for _, tmpl := range []string{"edit", "view"} {
-		templates[tmpl] = template.MustParseFile(tmpl+".html", nil)
+		t := template.Must(template.ParseFile(tmpl + ".html"))
+		templates[tmpl] = t
 	}
 }
 
@@ -763,10 +766,9 @@ var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")

The function regexp.MustCompile will parse and compile the regular expression, and return a regexp.Regexp. -MustCompile, like template.MustParseFile, -is distinct from Compile in that it will panic if -the expression compilation fails, while Compile returns an -os.Error as a second parameter. +MustCompile is distinct from Compile in that it will +panic if the expression compilation fails, while Compile returns +an os.Error as a second parameter.

diff --git a/doc/codelab/wiki/srcextract.go b/doc/codelab/wiki/srcextract.go index 60d4303df9..6b5fbcb432 100644 --- a/doc/codelab/wiki/srcextract.go +++ b/doc/codelab/wiki/srcextract.go @@ -8,7 +8,7 @@ import ( "go/ast" "go/token" "log" - "old/template" + "template" "os" ) diff --git a/doc/codelab/wiki/view.html b/doc/codelab/wiki/view.html index ca2ffc20b6..0233915774 100644 --- a/doc/codelab/wiki/view.html +++ b/doc/codelab/wiki/view.html @@ -1,5 +1,5 @@ -

{Title}

+

{{.Title |html}}

-

[edit]

+

[edit]

-
{Body}
+
{{printf "%s" .Body |html}}
diff --git a/doc/codelab/wiki/wiki.html b/doc/codelab/wiki/wiki.html index 91221bd616..634babd8b8 100644 --- a/doc/codelab/wiki/wiki.html +++ b/doc/codelab/wiki/wiki.html @@ -7,7 +7,7 @@ Covered in this codelab: @@ -366,27 +366,27 @@ This function will work fine, but all that hard-coded HTML is ugly. Of course, there is a better way.

-

The old/template package

+

The template package

-The old/template package is part of the Go standard library. +The template package is part of the Go standard library. (A new template package is coming; this code lab will be updated soon.) We can -use old/template to keep the HTML in a separate file, allowing +use template to keep the HTML in a separate file, allowing us to change the layout of our edit page without modifying the underlying Go code.

-First, we must add old/template to the list of imports: +First, we must add template to the list of imports:

 import (
 	"http"
 	"io/ioutil"
-	"old/template"
 	"os"
+	"template"
 )
 
@@ -414,19 +414,21 @@ The function template.ParseFile will read the contents of

-The method t.Execute replaces all occurrences of -{Title} and {Body} with the values of -p.Title and p.Body, and writes the resultant -HTML to the http.ResponseWriter. +The method t.Execute executes the template, writing the +generated HTML to the http.ResponseWriter. +The .Title and .Body dotted identifiers refer to +p.Title and p.Body.

-Note that we've used {Body|html} in the above template. -The |html part asks the template engine to pass the value -Body through the html formatter before outputting it, -which escapes HTML characters (such as replacing > with -&gt;). -This will prevent user data from corrupting the form HTML. +Template directives are enclosed in double curly braces. +The printf "%s" .Body instruction is a function call +that outputs .Body as a string instead of a stream of bytes, +the same as a call to fmt.Printf. +The |html part of each directive pipes the value through the +html formatter before outputting it, which escapes HTML +characters (such as replacing > with &gt;), +preventing user data from corrupting the form HTML.

@@ -572,10 +574,10 @@ our *Template values, keyed by string

Then we create an init function, which will be called before main at program initialization. The function -template.MustParseFile is a convenience wrapper around -ParseFile that does not return an error code; instead, it panics -if an error is encountered. A panic is appropriate here; if the templates can't -be loaded the only sensible thing to do is exit the program. +template.Must is a convenience wrapper that panics when passed a +non-nil os.Error value, and otherwise returns the +*Template unaltered. A panic is appropriate here; if the templates +can't be loaded the only sensible thing to do is exit the program.

@@ -618,10 +620,9 @@ Then we can create a global variable to store our validation regexp:
 

The function regexp.MustCompile will parse and compile the regular expression, and return a regexp.Regexp. -MustCompile, like template.MustParseFile, -is distinct from Compile in that it will panic if -the expression compilation fails, while Compile returns an -os.Error as a second parameter. +MustCompile is distinct from Compile in that it will +panic if the expression compilation fails, while Compile returns +an os.Error as a second parameter.

-- 2.50.0