From f3fc0090f46b9e1cbcfb5f870d2d0ef59f6e4f31 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Thu, 13 Sep 2012 13:41:13 -0700 Subject: [PATCH] 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 fc793591b5..6ad7ee3c22 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 4ac745c930..861131ddf5 100644 --- a/doc/progs/eff_qr.go +++ b/doc/progs/eff_qr.go @@ -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 = ` {{if .}} - +
-{{html .}} +{{.}}

{{end}}