]> Cypherpunks repositories - gostls13.git/commitdiff
effective_go: use html/template instead of text/template
authorRob Pike <r@golang.org>
Thu, 13 Sep 2012 20:41:13 +0000 (13:41 -0700)
committerRob Pike <r@golang.org>
Thu, 13 Sep 2012 20:41:13 +0000 (13:41 -0700)
Should have done this a long time ago.
Fixes #3811.

R=golang-dev, adg, rsc
CC=golang-dev
https://golang.org/cl/6488120

doc/effective_go.html
doc/progs/eff_qr.go

index fc793591b55f8c1e7169f19db4709839cd0e05be..6ad7ee3c224e6aa1a00f089c053fcbfa8da62c71 100644 (file)
@@ -2992,11 +2992,11 @@ server; it blocks while the server runs.
 executes the template on the data in the form value named <code>s</code>.
 </p>
 <p>
-The template package is powerful;
+The template package <code>html/template</code> is powerful;
 this program just touches on its capabilities.
-In essence, it rewrites a piece of text on the fly by substituting elements derived
+In essence, it rewrites a piece of HTML text on the fly by substituting elements derived
 from data items passed to <code>templ.Execute</code>, in this case the
-form value.  
+form value.
 Within the template text (<code>templateStr</code>),
 double-brace-delimited pieces denote template actions.
 The piece from <code>{{html "{{if .}}"}}</code>
@@ -3005,13 +3005,14 @@ is non-empty.
 That is, when the string is empty, this piece of the template is suppressed.
 </p>
 <p>
-The snippet <code>{{html "{{urlquery .}}"}}</code> says to process the data with the function
-<code>urlquery</code>, which sanitizes the query string
-for safe display on the web page.
+The two snippets <code>{{html "{{.}}"}}</code> say to show the data presented to
+the template—the query string—on the web page.
+The HTML template package automatically provides appropriate escaping so the
+text is safe to display.
 </p>
 <p>
 The rest of the template string is just the HTML to show when the page loads.
-If this is too quick an explanation, see the <a href="/pkg/text/template/">documentation</a>
+If this is too quick an explanation, see the <a href="/pkg/html/template/">documentation</a>
 for the template package for a more thorough discussion.
 </p>
 <p>
index 4ac745c93079c08b31df28047a3ea485ed06f68e..861131ddf54b5ea8b708d2ce241ec7bf0ebfe4e0 100644 (file)
@@ -8,9 +8,9 @@ package main
 
 import (
        "flag"
+       "html/template"
        "log"
        "net/http"
-       "text/template"
 )
 
 var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
@@ -37,9 +37,9 @@ const templateStr = `
 </head>
 <body>
 {{if .}}
-<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{urlquery .}}" />
+<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{.}}" />
 <br>
-{{html .}}
+{{.}}
 <br>
 <br>
 {{end}}