mirror of
https://github.com/golang/go
synced 2024-11-26 14:08:37 -07:00
change template function interface to
func(w io.Write, value interface{}, format string) R=r DELTA=16 (3 added, 3 deleted, 10 changed) OCL=27399 CL=27401
This commit is contained in:
parent
a20a50b0b2
commit
6d617a881a
@ -8,21 +8,20 @@ package template
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt";
|
"fmt";
|
||||||
|
"io";
|
||||||
"reflect";
|
"reflect";
|
||||||
)
|
)
|
||||||
|
|
||||||
// HtmlFormatter formats arbitrary values for HTML
|
// HtmlFormatter formats arbitrary values for HTML
|
||||||
// TODO: do something for real.
|
// TODO: do something for real.
|
||||||
func HtmlFormatter(v reflect.Value) string {
|
func HtmlFormatter(w io.Write, value interface{}, format string) {
|
||||||
s := fmt.Sprint(reflect.Indirect(v).Interface());
|
fmt.Fprint(w, value);
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringFormatter formats returns the default string representation.
|
// StringFormatter formats returns the default string representation.
|
||||||
// It is stored under the name "str" and is the default formatter.
|
// It is stored under the name "str" and is the default formatter.
|
||||||
// You can override the default formatter by storing your default
|
// You can override the default formatter by storing your default
|
||||||
// under the name "" in your custom formatter map.
|
// under the name "" in your custom formatter map.
|
||||||
func StringFormatter(v reflect.Value) string {
|
func StringFormatter(w io.Write, value interface{}, format string) {
|
||||||
s := fmt.Sprint(reflect.Indirect(v).Interface());
|
fmt.Fprint(w, value);
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,7 @@ const (
|
|||||||
|
|
||||||
// FormatterMap is the type describing the mapping from formatter
|
// FormatterMap is the type describing the mapping from formatter
|
||||||
// names to the functions that implement them.
|
// names to the functions that implement them.
|
||||||
// TODO(rsc): Maybe func should take interface{} instead?
|
type FormatterMap map[string] func(io.Write, interface{}, string)
|
||||||
type FormatterMap map[string] func(reflect.Value) string
|
|
||||||
|
|
||||||
// Built-in formatters.
|
// Built-in formatters.
|
||||||
var builtins = FormatterMap {
|
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.
|
// Evalute a variable, looking up through the parent if necessary.
|
||||||
// If it has a formatter attached ({var|formatter}) run that too.
|
// 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;
|
name := name_formatter;
|
||||||
formatter := "";
|
formatter := "";
|
||||||
bar := strings.Index(name_formatter, "|");
|
bar := strings.Index(name_formatter, "|");
|
||||||
@ -445,16 +444,18 @@ func (t *template) evalVariable(name_formatter string) string {
|
|||||||
name = name_formatter[0:bar];
|
name = name_formatter[0:bar];
|
||||||
formatter = name_formatter[bar+1:len(name_formatter)];
|
formatter = name_formatter[bar+1:len(name_formatter)];
|
||||||
}
|
}
|
||||||
val := t.varValue(name);
|
val := t.varValue(name).Interface();
|
||||||
// is it in user-supplied map?
|
// is it in user-supplied map?
|
||||||
if t.fmap != nil {
|
if t.fmap != nil {
|
||||||
if fn, ok := t.fmap[formatter]; ok {
|
if fn, ok := t.fmap[formatter]; ok {
|
||||||
return fn(val)
|
fn(w, val, formatter);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// is it in builtin map?
|
// is it in builtin map?
|
||||||
if fn, ok := builtins[formatter]; ok {
|
if fn, ok := builtins[formatter]; ok {
|
||||||
return fn(val)
|
fn(w, val, formatter);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
t.error(ErrNoFormatter, ": ", formatter);
|
t.error(ErrNoFormatter, ": ", formatter);
|
||||||
panic("notreached");
|
panic("notreached");
|
||||||
@ -484,7 +485,7 @@ func (t *template) execute() {
|
|||||||
panic("unknown literal: ", w[0]);
|
panic("unknown literal: ", w[0]);
|
||||||
}
|
}
|
||||||
case Variable:
|
case Variable:
|
||||||
t.wr.Write(io.StringBytes(t.evalVariable(w[0])));
|
t.writeVariable(t.wr, w[0]);
|
||||||
case Or, End, Alternates:
|
case Or, End, Alternates:
|
||||||
t.error(ErrSyntax, ": ", string(item));
|
t.error(ErrSyntax, ": ", string(item));
|
||||||
case Section:
|
case Section:
|
||||||
|
Loading…
Reference in New Issue
Block a user