]> Cypherpunks repositories - gostls13.git/commitdiff
change template function interface to
authorRuss Cox <rsc@golang.org>
Tue, 14 Apr 2009 02:29:23 +0000 (19:29 -0700)
committerRuss Cox <rsc@golang.org>
Tue, 14 Apr 2009 02:29:23 +0000 (19:29 -0700)
func(w io.Write, value interface{}, format string)

R=r
DELTA=16  (3 added, 3 deleted, 10 changed)
OCL=27399
CL=27401

src/lib/template/format.go
src/lib/template/template.go

index ee90fed6165622f1baeea07d398af332ae49bc4c..1dd9aebdec84b81cf20f479b53f23a7efd5b372e 100644 (file)
@@ -8,21 +8,20 @@ package template
 
 import (
        "fmt";
+       "io";
        "reflect";
 )
 
 // HtmlFormatter formats arbitrary values for HTML
 // TODO: do something for real.
-func HtmlFormatter(v reflect.Value) string {
-       s := fmt.Sprint(reflect.Indirect(v).Interface());
-       return s;
+func HtmlFormatter(w io.Write, value interface{}, format string) {
+       fmt.Fprint(w, value);
 }
 
 // StringFormatter formats returns the default string representation.
 // It is stored under the name "str" and is the default formatter.
 // You can override the default formatter by storing your default
 // under the name "" in your custom formatter map.
-func StringFormatter(v reflect.Value) string {
-       s := fmt.Sprint(reflect.Indirect(v).Interface());
-       return s;
+func StringFormatter(w io.Write, value interface{}, format string) {
+       fmt.Fprint(w, value);
 }
index a40a8b86ca7d1724e09b557d30cb44e15dca4c31..274d9bf886351e3414e513d629cc972eabf66044 100644 (file)
@@ -48,8 +48,7 @@ const (
 
 // FormatterMap is the type describing the mapping from formatter
 // names to the functions that implement them.
-// TODO(rsc): Maybe func should take interface{} instead?
-type FormatterMap map[string] func(reflect.Value) string
+type FormatterMap map[string] func(io.Write, interface{}, string)
 
 // Built-in formatters.
 var builtins = FormatterMap {
@@ -437,7 +436,7 @@ func (t *template) varValue(name string) reflect.Value {
 
 // Evalute a variable, looking up through the parent if necessary.
 // If it has a formatter attached ({var|formatter}) run that too.
-func (t *template) evalVariable(name_formatter string) string {
+func (t *template) writeVariable(w io.Write, name_formatter string) {
        name := name_formatter;
        formatter := "";
        bar := strings.Index(name_formatter, "|");
@@ -445,16 +444,18 @@ func (t *template) evalVariable(name_formatter string) string {
                name = name_formatter[0:bar];
                formatter = name_formatter[bar+1:len(name_formatter)];
        }
-       val := t.varValue(name);
+       val := t.varValue(name).Interface();
        // is it in user-supplied map?
        if t.fmap != nil {
                if fn, ok := t.fmap[formatter]; ok {
-                       return fn(val)
+                       fn(w, val, formatter);
+                       return;
                }
        }
        // is it in builtin map?
        if fn, ok := builtins[formatter]; ok {
-               return fn(val)
+               fn(w, val, formatter);
+               return;
        }
        t.error(ErrNoFormatter, ": ", formatter);
        panic("notreached");
@@ -484,7 +485,7 @@ func (t *template) execute() {
                                panic("unknown literal: ", w[0]);
                        }
                case Variable:
-                       t.wr.Write(io.StringBytes(t.evalVariable(w[0])));
+                       t.writeVariable(t.wr, w[0]);
                case Or, End, Alternates:
                        t.error(ErrSyntax, ": ", string(item));
                case Section: