From b7f91ba06f79ec5cd278ac8e4a14c5d88130c3f6 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sat, 22 Sep 2012 05:55:11 +1000 Subject: [release-branch.go1] effective_go: use html/template instead of text/template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ««« backport e3fce06291ec effective_go: use html/template instead of text/template 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 | 15 ++++++++------- doc/progs/eff_qr.go | 6 +++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/doc/effective_go.html b/doc/effective_go.html index 6cacac6303..d38b781b45 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -2992,11 +2992,11 @@ server; it blocks while the server runs. executes the template on the data in the form value named s.

-The template package is powerful; +The template package html/template 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 templ.Execute, in this case the -form value. +form value. Within the template text (templateStr), double-brace-delimited pieces denote template actions. The piece from {{html "{{if .}}"}} @@ -3005,13 +3005,14 @@ is non-empty. That is, when the string is empty, this piece of the template is suppressed.

-The snippet {{html "{{urlquery .}}"}} says to process the data with the function -urlquery, which sanitizes the query string -for safe display on the web page. +The two snippets {{html "{{.}}"}} 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.

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 documentation +If this is too quick an explanation, see the documentation for the template package for a more thorough discussion.

diff --git a/doc/progs/eff_qr.go b/doc/progs/eff_qr.go index de96a0208f..0733bb1637 100644 --- a/doc/progs/eff_qr.go +++ b/doc/progs/eff_qr.go @@ -2,9 +2,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 @@ -31,9 +31,9 @@ const templateStr = ` {{if .}} - +
-{{html .}} +{{.}}

{{end}} -- cgit v1.2.3-54-g00ecf