The HTML one here is just a stub - should use an HTML library to do the right thing.
R=rsc
DELTA=54 (47 added, 2 deleted, 5 changed)
OCL=27250
CL=27250
sync.dirinstall:
syscall.dirinstall: sync.dirinstall
tabwriter.dirinstall: os.dirinstall io.dirinstall container.dirinstall
-template.dirinstall: bufio.install fmt.dirinstall io.dirinstall os.dirinstall reflect.dirinstall strings.install
+template.dirinstall: fmt.dirinstall io.dirinstall os.dirinstall reflect.dirinstall strings.install
time.dirinstall: once.install os.dirinstall io.dirinstall
$(AS) $*.s
O1=\
+ format.$O\
+
+O2=\
template.$O\
-template.a: a1
+template.a: a1 a2
a1: $(O1)
- $(AR) grc template.a template.$O
+ $(AR) grc template.a format.$O
rm -f $(O1)
+a2: $(O2)
+ $(AR) grc template.a template.$O
+ rm -f $(O2)
+
newpkg: clean
$(AR) grc template.a
$(O1): newpkg
+$(O2): a1
nuke: clean
rm -f $(GOROOT)/pkg/template.a
--- /dev/null
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Template library: default formatters
+
+package template
+
+import (
+ "fmt";
+ "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;
+}
+
+// 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;
+}
// Template library. See http://code.google.com/p/json-template/wiki/Reference
// TODO: document this here as well.
-
package template
import (
- "bufio";
"fmt";
"io";
"os";
"reflect";
"strings";
+ "template";
)
var ErrLBrace = os.NewError("unexpected opening brace")
// names to the functions that implement them.
type FormatterMap map[string] func(reflect.Value) string
+// Built-in formatters.
+var builtins = FormatterMap {
+ "html" : HtmlFormatter,
+ "str" : StringFormatter,
+ "" : StringFormatter,
+}
+
type template struct {
errorchan chan *os.Error; // for erroring out
linenum *int; // shared by all templates derived from this one
formatter = name_formatter[bar+1:len(name_formatter)];
}
val := t.varValue(name);
+ // is it in user-supplied map?
if fn, ok := t.fmap[formatter]; ok {
return fn(val)
}
- if formatter == "" {
- return fmt.Sprint(val.Interface())
+ // is it in builtin map?
+ if fn, ok := builtins[formatter]; ok {
+ return fn(val)
}
t.error(ErrNoFormatter, ": ", formatter);
panic("notreached");
&Test{
"{.section pdata }\n"
"{header|uppercase}={integer|+1}\n"
+ "{header|html}={integer|str}\n"
"{.end}\n",
"HEADER=78\n"
+ "Header=77\n"
},
}