]> Cypherpunks repositories - gostls13.git/commitdiff
Start list of default formatters for template variables.
authorRob Pike <r@golang.org>
Thu, 9 Apr 2009 07:10:46 +0000 (00:10 -0700)
committerRob Pike <r@golang.org>
Thu, 9 Apr 2009 07:10:46 +0000 (00:10 -0700)
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

src/lib/Makefile
src/lib/template/Makefile
src/lib/template/format.go [new file with mode: 0644]
src/lib/template/template.go
src/lib/template/template_test.go

index 6b485e3fdd1bcc38e36cde6536c30960a8cc99b4..367265f228fc9e8081e3f025ffc79946d6399718 100644 (file)
@@ -120,6 +120,6 @@ strconv.dirinstall: math.dirinstall os.dirinstall utf8.install
 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
 
index 856ba185eb2896513a8211a5302891e963ef53d0..2634af433bdb40f0212b3816da6597f73ffd437e 100644 (file)
@@ -32,18 +32,26 @@ coverage: packages
        $(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
diff --git a/src/lib/template/format.go b/src/lib/template/format.go
new file mode 100644 (file)
index 0000000..ee90fed
--- /dev/null
@@ -0,0 +1,28 @@
+// 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;
+}
index fd6a863d25f94208ae31f5833e62726650c9adfd..1702889f4d733ab01f00f94436838a2811009090 100644 (file)
@@ -4,16 +4,15 @@
 
 // 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")
@@ -51,6 +50,13 @@ const (
 // 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
@@ -439,11 +445,13 @@ func (t *template) evalVariable(name_formatter string) string {
                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");
index a67a888ea011112ec996bbf04de87bcd40382bb0..ef9b28ab81b327d353ff26ee6d18725afc14ab7e 100644 (file)
@@ -143,9 +143,11 @@ var tests = []*Test {
        &Test{
                "{.section pdata }\n"
                "{header|uppercase}={integer|+1}\n"
+               "{header|html}={integer|str}\n"
                "{.end}\n",
 
                "HEADER=78\n"
+               "Header=77\n"
        },
 }