1
0
mirror of https://github.com/golang/go synced 2024-11-21 18:14:42 -07:00

template: reverse order of arguments to Execute

In line with other functions such as Fprintf, put the
thing to be written first.

Apologies for the breakages this is sure to cause.

R=rsc, gri, adg, eds, r2, aam
CC=golang-dev
https://golang.org/cl/4169042
This commit is contained in:
Rob Pike 2011-02-09 14:23:01 -08:00
parent 2a81292ac3
commit fb9e37cd9b
12 changed files with 25 additions and 25 deletions

View File

@ -64,7 +64,7 @@ func init() {
} }
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates[tmpl].Execute(p, w) err := templates[tmpl].Execute(w, p)
if err != nil { if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError) http.Error(w, err.String(), http.StatusInternalServerError)
} }

View File

@ -734,7 +734,7 @@ the <code>Execute</code> method on the appropriate <code>Template</code> from
<pre> <pre>
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates[tmpl].Execute(p, w) err := templates[tmpl].Execute(w, p)
if err != nil { if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError) http.Error(w, err.String(), http.StatusInternalServerError)
} }

View File

@ -2885,7 +2885,7 @@ func main() {
} }
func QR(w http.ResponseWriter, req *http.Request) { func QR(w http.ResponseWriter, req *http.Request) {
templ.Execute(req.FormValue("s"), w) templ.Execute(w, req.FormValue("s"))
} }
func UrlHtmlFormatter(w io.Writer, fmt string, v ...interface{}) { func UrlHtmlFormatter(w io.Writer, fmt string, v ...interface{}) {

View File

@ -66,7 +66,7 @@ func FrontPage(w http.ResponseWriter, req *http.Request) {
if err != nil { if err != nil {
data = helloWorld data = helloWorld
} }
frontPage.Execute(data, w) frontPage.Execute(w, data)
} }
// Compile is an HTTP handler that reads Go source code from the request, // Compile is an HTTP handler that reads Go source code from the request,
@ -123,7 +123,7 @@ func Compile(w http.ResponseWriter, req *http.Request) {
if *htmlOutput { if *htmlOutput {
w.Write(out) w.Write(out)
} else { } else {
output.Execute(out, w) output.Execute(w, out)
} }
} }
@ -132,9 +132,9 @@ func Compile(w http.ResponseWriter, req *http.Request) {
func error(w http.ResponseWriter, out []byte, err os.Error) { func error(w http.ResponseWriter, out []byte, err os.Error) {
w.WriteHeader(404) w.WriteHeader(404)
if out != nil { if out != nil {
output.Execute(out, w) output.Execute(w, out)
} else { } else {
output.Execute(err.String(), w) output.Execute(w, err.String())
} }
} }

View File

@ -674,7 +674,7 @@ func servePage(w http.ResponseWriter, title, subtitle, query string, content []b
content, content,
} }
if err := godocHTML.Execute(&d, w); err != nil { if err := godocHTML.Execute(w, &d); err != nil {
log.Printf("godocHTML.Execute: %s", err) log.Printf("godocHTML.Execute: %s", err)
} }
} }
@ -742,7 +742,7 @@ func serveHTMLDoc(w http.ResponseWriter, r *http.Request, abspath, relpath strin
func applyTemplate(t *template.Template, name string, data interface{}) []byte { func applyTemplate(t *template.Template, name string, data interface{}) []byte {
var buf bytes.Buffer var buf bytes.Buffer
if err := t.Execute(data, &buf); err != nil { if err := t.Execute(&buf, data); err != nil {
log.Printf("%s.Execute: %s", name, err) log.Printf("%s.Execute: %s", name, err)
} }
return buf.Bytes() return buf.Bytes()

View File

@ -381,7 +381,7 @@ func main() {
} }
} }
if err := packageText.Execute(info, os.Stdout); err != nil { if err := packageText.Execute(os.Stdout, info); err != nil {
log.Printf("packageText.Execute: %s", err) log.Printf("packageText.Execute: %s", err)
} }
} }

View File

@ -75,7 +75,7 @@ func makeMakefile(dir, pkg string) ([]byte, os.Error) {
var buf bytes.Buffer var buf bytes.Buffer
md := makedata{pkg, goFiles, cgoFiles, oFiles} md := makedata{pkg, goFiles, cgoFiles, oFiles}
if err := makefileTemplate.Execute(&md, &buf); err != nil { if err := makefileTemplate.Execute(&buf, &md); err != nil {
return nil, err return nil, err
} }
return buf.Bytes(), nil return buf.Bytes(), nil

View File

@ -368,7 +368,7 @@ func main() {
if err != nil { if err != nil {
log.Exit(err) log.Exit(err)
} }
err = t.Execute(data, os.Stdout) err = t.Execute(os.Stdout, data)
if err != nil { if err != nil {
log.Exit(err) log.Exit(err)
} }

View File

@ -83,7 +83,7 @@ func (server debugHTTP) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
server.Unlock() server.Unlock()
sort.Sort(services) sort.Sort(services)
err := debug.Execute(services, w) err := debug.Execute(w, services)
if err != nil { if err != nil {
fmt.Fprintln(w, "rpc: error executing template:", err.String()) fmt.Fprintln(w, "rpc: error executing template:", err.String())
} }

View File

@ -972,7 +972,7 @@ func (t *Template) ParseFile(filename string) (err os.Error) {
// Execute applies a parsed template to the specified data object, // Execute applies a parsed template to the specified data object,
// generating output to wr. // generating output to wr.
func (t *Template) Execute(data interface{}, wr io.Writer) (err os.Error) { func (t *Template) Execute(wr io.Writer, data interface{}) (err os.Error) {
// Extract the driver data. // Extract the driver data.
val := reflect.NewValue(data) val := reflect.NewValue(data)
defer checkError(&err) defer checkError(&err)

View File

@ -492,7 +492,7 @@ func testAll(t *testing.T, parseFunc func(*Test) (*Template, os.Error)) {
t.Error("unexpected parse error: ", err) t.Error("unexpected parse error: ", err)
continue continue
} }
err = tmpl.Execute(s, &buf) err = tmpl.Execute(&buf, s)
if test.err == "" { if test.err == "" {
if err != nil { if err != nil {
t.Error("unexpected execute error:", err) t.Error("unexpected execute error:", err)
@ -517,7 +517,7 @@ func TestMapDriverType(t *testing.T) {
t.Error("unexpected parse error:", err) t.Error("unexpected parse error:", err)
} }
var b bytes.Buffer var b bytes.Buffer
err = tmpl.Execute(mp, &b) err = tmpl.Execute(&b, mp)
if err != nil { if err != nil {
t.Error("unexpected execute error:", err) t.Error("unexpected execute error:", err)
} }
@ -535,7 +535,7 @@ func TestMapNoEntry(t *testing.T) {
t.Error("unexpected parse error:", err) t.Error("unexpected parse error:", err)
} }
var b bytes.Buffer var b bytes.Buffer
err = tmpl.Execute(mp, &b) err = tmpl.Execute(&b, mp)
if err != nil { if err != nil {
t.Error("unexpected execute error:", err) t.Error("unexpected execute error:", err)
} }
@ -552,7 +552,7 @@ func TestStringDriverType(t *testing.T) {
t.Error("unexpected parse error:", err) t.Error("unexpected parse error:", err)
} }
var b bytes.Buffer var b bytes.Buffer
err = tmpl.Execute("hello", &b) err = tmpl.Execute(&b, "hello")
if err != nil { if err != nil {
t.Error("unexpected execute error:", err) t.Error("unexpected execute error:", err)
} }
@ -569,7 +569,7 @@ func TestTwice(t *testing.T) {
t.Error("unexpected parse error:", err) t.Error("unexpected parse error:", err)
} }
var b bytes.Buffer var b bytes.Buffer
err = tmpl.Execute("hello", &b) err = tmpl.Execute(&b, "hello")
if err != nil { if err != nil {
t.Error("unexpected parse error:", err) t.Error("unexpected parse error:", err)
} }
@ -578,7 +578,7 @@ func TestTwice(t *testing.T) {
if s != expect { if s != expect {
t.Errorf("failed passing string as data: expected %q got %q", expect, s) t.Errorf("failed passing string as data: expected %q got %q", expect, s)
} }
err = tmpl.Execute("hello", &b) err = tmpl.Execute(&b, "hello")
if err != nil { if err != nil {
t.Error("unexpected parse error:", err) t.Error("unexpected parse error:", err)
} }
@ -614,7 +614,7 @@ func TestCustomDelims(t *testing.T) {
continue continue
} }
var b bytes.Buffer var b bytes.Buffer
err = tmpl.Execute("hello", &b) err = tmpl.Execute(&b, "hello")
s := b.String() s := b.String()
if s != "template: hello"+ldelim+rdelim { if s != "template: hello"+ldelim+rdelim {
t.Errorf("failed delim check(%q %q) %q got %q", ldelim, rdelim, text, s) t.Errorf("failed delim check(%q %q) %q got %q", ldelim, rdelim, text, s)
@ -635,7 +635,7 @@ func TestVarIndirection(t *testing.T) {
if err != nil { if err != nil {
t.Fatal("unexpected parse error:", err) t.Fatal("unexpected parse error:", err)
} }
err = tmpl.Execute(s, &buf) err = tmpl.Execute(&buf, s)
if err != nil { if err != nil {
t.Fatal("unexpected execute error:", err) t.Fatal("unexpected execute error:", err)
} }
@ -669,7 +669,7 @@ func TestReferenceToUnexported(t *testing.T) {
if err != nil { if err != nil {
t.Fatal("unexpected parse error:", err) t.Fatal("unexpected parse error:", err)
} }
err = tmpl.Execute(u, &buf) err = tmpl.Execute(&buf, u)
if err == nil { if err == nil {
t.Fatal("expected execute error, got none") t.Fatal("expected execute error, got none")
} }
@ -749,7 +749,7 @@ func TestFormatters(t *testing.T) {
continue continue
} }
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
err = tmpl.Execute(data, buf) err = tmpl.Execute(buf, data)
if err != nil { if err != nil {
t.Error("unexpected Execute error: ", err) t.Error("unexpected Execute error: ", err)
continue continue

View File

@ -49,7 +49,7 @@ func main() {
} }
func run(t *template.Template, a interface{}, out io.Writer) { func run(t *template.Template, a interface{}, out io.Writer) {
if err := t.Execute(a, out); err != nil { if err := t.Execute(out, a); err != nil {
panic(err) panic(err)
} }
} }