summaryrefslogtreecommitdiff
path: root/doc/codelab
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-09-13 13:13:44 +0200
committerOndřej Surý <ondrej@sury.org>2011-09-13 13:13:44 +0200
commit9464a0c36318f8a801c07d6874bd0cea40f12504 (patch)
treef0178491c19d4f1ebc7b92eede86690998466480 /doc/codelab
parentba9fda6068cfadd42db0b152fdca7e8b67aaf77d (diff)
parent5ff4c17907d5b19510a62e08fd8d3b11e62b431d (diff)
downloadgolang-9464a0c36318f8a801c07d6874bd0cea40f12504.tar.gz
Merge commit 'upstream/60' into debian-sid
Diffstat (limited to 'doc/codelab')
-rw-r--r--doc/codelab/wiki/edit.html6
-rw-r--r--doc/codelab/wiki/final-noclosure.go2
-rw-r--r--doc/codelab/wiki/final-noerror.go4
-rw-r--r--doc/codelab/wiki/final.go3
-rw-r--r--doc/codelab/wiki/htmlify.go4
-rw-r--r--doc/codelab/wiki/index.html60
-rw-r--r--doc/codelab/wiki/srcextract.go2
-rw-r--r--doc/codelab/wiki/view.html6
-rw-r--r--doc/codelab/wiki/wiki.html41
9 files changed, 68 insertions, 60 deletions
diff --git a/doc/codelab/wiki/edit.html b/doc/codelab/wiki/edit.html
index 7a5768ce9..c14953b17 100644
--- a/doc/codelab/wiki/edit.html
+++ b/doc/codelab/wiki/edit.html
@@ -1,6 +1,6 @@
-<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>
diff --git a/doc/codelab/wiki/final-noclosure.go b/doc/codelab/wiki/final-noclosure.go
index d09a0d7ab..067f502c6 100644
--- a/doc/codelab/wiki/final-noclosure.go
+++ b/doc/codelab/wiki/final-noclosure.go
@@ -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 5fcf1de76..b8edbee9b 100644
--- a/doc/codelab/wiki/final-noerror.go
+++ b/doc/codelab/wiki/final-noerror.go
@@ -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 c97a699d4..47a4c3473 100644
--- a/doc/codelab/wiki/final.go
+++ b/doc/codelab/wiki/final.go
@@ -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 456d06fd5..9e7605b92 100644
--- a/doc/codelab/wiki/htmlify.go
+++ b/doc/codelab/wiki/htmlify.go
@@ -1,12 +1,12 @@
package main
import (
- "os"
"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 cc187ad90..50e9db5e9 100644
--- a/doc/codelab/wiki/index.html
+++ b/doc/codelab/wiki/index.html
@@ -429,7 +429,9 @@ Of course, there is a better way.
<h2>The <code>template</code> package</h2>
<p>
-The <code>template</code> package is part of the Go standard library. We can
+The <code>template</code> package is part of the Go standard library.
+(A new template package is coming; this code lab will be updated soon.)
+We can
use <code>template</code> to keep the HTML in a separate file, allowing
us to change the layout of our edit page without modifying the underlying Go
code.
@@ -454,10 +456,10 @@ Open a new file named <code>edit.html</code>, and add the following lines:
</p>
<pre>
-&lt;h1&gt;Editing {Title}&lt;/h1&gt;
+&lt;h1&gt;Editing {{.Title |html}}&lt;/h1&gt;
-&lt;form action=&#34;/save/{Title}&#34; method=&#34;POST&#34;&gt;
-&lt;div&gt;&lt;textarea name=&#34;body&#34; rows=&#34;20&#34; cols=&#34;80&#34;&gt;{Body|html}&lt;/textarea&gt;&lt;/div&gt;
+&lt;form action=&#34;/save/{{.Title |html}}&#34; method=&#34;POST&#34;&gt;
+&lt;div&gt;&lt;textarea name=&#34;body&#34; rows=&#34;20&#34; cols=&#34;80&#34;&gt;{{printf &#34;%s&#34; .Body |html}}&lt;/textarea&gt;&lt;/div&gt;
&lt;div&gt;&lt;input type=&#34;submit&#34; value=&#34;Save&#34;&gt;&lt;/div&gt;
&lt;/form&gt;
</pre>
@@ -474,7 +476,7 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
p = &amp;Page{Title: title}
}
- t, _ := template.ParseFile(&#34;edit.html&#34;, nil)
+ t, _ := template.ParseFile(&#34;edit.html&#34;)
t.Execute(w, p)
}
</pre>
@@ -485,19 +487,21 @@ The function <code>template.ParseFile</code> will read the contents of
</p>
<p>
-The method <code>t.Execute</code> replaces all occurrences of
-<code>{Title}</code> and <code>{Body}</code> with the values of
-<code>p.Title</code> and <code>p.Body</code>, and writes the resultant
-HTML to the <code>http.ResponseWriter</code>.
+The method <code>t.Execute</code> executes the template, writing the
+generated HTML to the <code>http.ResponseWriter</code>.
+The <code>.Title</code> and <code>.Body</code> dotted identifiers refer to
+<code>p.Title</code> and <code>p.Body</code>.
</p>
<p>
-Note that we've used <code>{Body|html}</code> in the above template.
-The <code>|html</code> part asks the template engine to pass the value
-<code>Body</code> through the <code>html</code> formatter before outputting it,
-which escapes HTML characters (such as replacing <code>&gt;</code> with
-<code>&amp;gt;</code>).
-This will prevent user data from corrupting the form HTML.
+Template directives are enclosed in double curly braces.
+The <code>printf "%s" .Body</code> instruction is a function call
+that outputs <code>.Body</code> as a string instead of a stream of bytes,
+the same as a call to <code>fmt.Printf</code>.
+The <code>|html</code> part of each directive pipes the value through the
+<code>html</code> formatter before outputting it, which escapes HTML
+characters (such as replacing <code>&gt;</code> with <code>&amp;gt;</code>),
+preventing user data from corrupting the form HTML.
</p>
<p>
@@ -511,11 +515,11 @@ While we're working with templates, let's create a template for our
</p>
<pre>
-&lt;h1&gt;{Title}&lt;/h1&gt;
+&lt;h1&gt;{{.Title |html}}&lt;/h1&gt;
-&lt;p&gt;[&lt;a href=&#34;/edit/{Title}&#34;&gt;edit&lt;/a&gt;]&lt;/p&gt;
+&lt;p&gt;[&lt;a href=&#34;/edit/{{.Title |html}}&#34;&gt;edit&lt;/a&gt;]&lt;/p&gt;
-&lt;div&gt;{Body}&lt;/div&gt;
+&lt;div&gt;{{printf &#34;%s&#34; .Body |html}}&lt;/div&gt;
</pre>
<p>
@@ -526,7 +530,7 @@ Modify <code>viewHandler</code> accordingly:
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
p, _ := loadPage(title)
- t, _ := template.ParseFile(&#34;view.html&#34;, nil)
+ t, _ := template.ParseFile(&#34;view.html&#34;)
t.Execute(w, p)
}
</pre>
@@ -706,16 +710,17 @@ var templates = make(map[string]*template.Template)
<p>
Then we create an <code>init</code> function, which will be called before
<code>main</code> at program initialization. The function
-<code>template.MustParseFile</code> is a convenience wrapper around
-<code>ParseFile</code> 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.
+<code>template.Must</code> is a convenience wrapper that panics when passed a
+non-nil <code>os.Error</code> value, and otherwise returns the
+<code>*Template</code> unaltered. A panic is appropriate here; if the templates
+can't be loaded the only sensible thing to do is exit the program.
</p>
<pre>
func init() {
for _, tmpl := range []string{&#34;edit&#34;, &#34;view&#34;} {
- templates[tmpl] = template.MustParseFile(tmpl+&#34;.html&#34;, nil)
+ t := template.Must(template.ParseFile(tmpl + &#34;.html&#34;))
+ templates[tmpl] = t
}
}
</pre>
@@ -761,10 +766,9 @@ var titleValidator = regexp.MustCompile(&#34;^[a-zA-Z0-9]+$&#34;)
<p>
The function <code>regexp.MustCompile</code> will parse and compile the
regular expression, and return a <code>regexp.Regexp</code>.
-<code>MustCompile</code>, like <code>template.MustParseFile</code>,
-is distinct from <code>Compile</code> in that it will panic if
-the expression compilation fails, while <code>Compile</code> returns an
-<code>os.Error</code> as a second parameter.
+<code>MustCompile</code> is distinct from <code>Compile</code> in that it will
+panic if the expression compilation fails, while <code>Compile</code> returns
+an <code>os.Error</code> as a second parameter.
</p>
<p>
diff --git a/doc/codelab/wiki/srcextract.go b/doc/codelab/wiki/srcextract.go
index 67294784e..6b5fbcb43 100644
--- a/doc/codelab/wiki/srcextract.go
+++ b/doc/codelab/wiki/srcextract.go
@@ -8,8 +8,8 @@ import (
"go/ast"
"go/token"
"log"
- "os"
"template"
+ "os"
)
var (
diff --git a/doc/codelab/wiki/view.html b/doc/codelab/wiki/view.html
index ca2ffc20b..023391577 100644
--- a/doc/codelab/wiki/view.html
+++ b/doc/codelab/wiki/view.html
@@ -1,5 +1,5 @@
-<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>
diff --git a/doc/codelab/wiki/wiki.html b/doc/codelab/wiki/wiki.html
index 4db880b9d..634babd8b 100644
--- a/doc/codelab/wiki/wiki.html
+++ b/doc/codelab/wiki/wiki.html
@@ -369,7 +369,9 @@ Of course, there is a better way.
<h2>The <code>template</code> package</h2>
<p>
-The <code>template</code> package is part of the Go standard library. We can
+The <code>template</code> package is part of the Go standard library.
+(A new template package is coming; this code lab will be updated soon.)
+We can
use <code>template</code> to keep the HTML in a separate file, allowing
us to change the layout of our edit page without modifying the underlying Go
code.
@@ -412,19 +414,21 @@ The function <code>template.ParseFile</code> will read the contents of
</p>
<p>
-The method <code>t.Execute</code> replaces all occurrences of
-<code>{Title}</code> and <code>{Body}</code> with the values of
-<code>p.Title</code> and <code>p.Body</code>, and writes the resultant
-HTML to the <code>http.ResponseWriter</code>.
+The method <code>t.Execute</code> executes the template, writing the
+generated HTML to the <code>http.ResponseWriter</code>.
+The <code>.Title</code> and <code>.Body</code> dotted identifiers refer to
+<code>p.Title</code> and <code>p.Body</code>.
</p>
<p>
-Note that we've used <code>{Body|html}</code> in the above template.
-The <code>|html</code> part asks the template engine to pass the value
-<code>Body</code> through the <code>html</code> formatter before outputting it,
-which escapes HTML characters (such as replacing <code>&gt;</code> with
-<code>&amp;gt;</code>).
-This will prevent user data from corrupting the form HTML.
+Template directives are enclosed in double curly braces.
+The <code>printf "%s" .Body</code> instruction is a function call
+that outputs <code>.Body</code> as a string instead of a stream of bytes,
+the same as a call to <code>fmt.Printf</code>.
+The <code>|html</code> part of each directive pipes the value through the
+<code>html</code> formatter before outputting it, which escapes HTML
+characters (such as replacing <code>&gt;</code> with <code>&amp;gt;</code>),
+preventing user data from corrupting the form HTML.
</p>
<p>
@@ -570,10 +574,10 @@ our <code>*Template</code> values, keyed by <code>string</code>
<p>
Then we create an <code>init</code> function, which will be called before
<code>main</code> at program initialization. The function
-<code>template.MustParseFile</code> is a convenience wrapper around
-<code>ParseFile</code> 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.
+<code>template.Must</code> is a convenience wrapper that panics when passed a
+non-nil <code>os.Error</code> value, and otherwise returns the
+<code>*Template</code> unaltered. A panic is appropriate here; if the templates
+can't be loaded the only sensible thing to do is exit the program.
</p>
<pre>
@@ -616,10 +620,9 @@ Then we can create a global variable to store our validation regexp:
<p>
The function <code>regexp.MustCompile</code> will parse and compile the
regular expression, and return a <code>regexp.Regexp</code>.
-<code>MustCompile</code>, like <code>template.MustParseFile</code>,
-is distinct from <code>Compile</code> in that it will panic if
-the expression compilation fails, while <code>Compile</code> returns an
-<code>os.Error</code> as a second parameter.
+<code>MustCompile</code> is distinct from <code>Compile</code> in that it will
+panic if the expression compilation fails, while <code>Compile</code> returns
+an <code>os.Error</code> as a second parameter.
</p>
<p>