1
0
mirror of https://github.com/golang/go synced 2024-11-21 20:44:39 -07:00

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
This commit is contained in:
Rob Pike 2012-09-13 13:41:13 -07:00
parent 032e5bfb30
commit f3fc0090f4
2 changed files with 11 additions and 10 deletions

View File

@ -2992,9 +2992,9 @@ server; it blocks while the server runs.
executes the template on the data in the form value named <code>s</code>. executes the template on the data in the form value named <code>s</code>.
</p> </p>
<p> <p>
The template package is powerful; The template package <code>html/template</code> is powerful;
this program just touches on its capabilities. 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 from data items passed to <code>templ.Execute</code>, in this case the
form value. form value.
Within the template text (<code>templateStr</code>), Within the template text (<code>templateStr</code>),
@ -3005,13 +3005,14 @@ is non-empty.
That is, when the string is empty, this piece of the template is suppressed. That is, when the string is empty, this piece of the template is suppressed.
</p> </p>
<p> <p>
The snippet <code>{{html "{{urlquery .}}"}}</code> says to process the data with the function The two snippets <code>{{html "{{.}}"}}</code> say to show the data presented to
<code>urlquery</code>, which sanitizes the query string the template—the query string—on the web page.
for safe display on the web page. The HTML template package automatically provides appropriate escaping so the
text is safe to display.
</p> </p>
<p> <p>
The rest of the template string is just the HTML to show when the page loads. 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. for the template package for a more thorough discussion.
</p> </p>
<p> <p>

View File

@ -8,9 +8,9 @@ package main
import ( import (
"flag" "flag"
"html/template"
"log" "log"
"net/http" "net/http"
"text/template"
) )
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18 var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
@ -37,9 +37,9 @@ const templateStr = `
</head> </head>
<body> <body>
{{if .}} {{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> <br>
{{html .}} {{.}}
<br> <br>
<br> <br>
{{end}} {{end}}