Skip to content

Commit 54b04d9

Browse files
committed
parse template function
1 parent 5a294ce commit 54b04d9

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Description
66

7-
**gotmpl** is a super simple command line program for rendering templates. **gotmpl** uses environment variables as its context and [go-adaptive-functions](https://github.com/spatialcurrent/go-adaptive-functions) for its functions.
7+
**gotmpl** is a super simple command line program for rendering templates. **gotmpl** uses environment variables as its context and [go-adaptive-functions](https://github.com/spatialcurrent/go-adaptive-functions) for its functions. **gotmpl** uses [go-simple-serializer](https://github.com/spatialcurrent/go-simple-serializer) for parsing text into data, such as in the "Render Time Table" example below.
88

99
# Installation
1010

@@ -32,6 +32,12 @@ echo '{{ split .SHELL "/" | last }}' | gotmpl
3232
echo '{{ .PATH | split ":" | set | array | sort | join ":" }}' | gotmpl
3333
```
3434

35+
**Render Time Table**
36+
37+
```shell
38+
echo '{{ with $items := .data | parse "csv" }}<table style="text-align:left;font-size:16px;"><tr><th>Time</th><th>Title</th>{{ range $items }}<tr><td>{{ .Time }}</td><td>{{ .Title }}</td></tr>{{ end }}</table>{{ end }}' | data=$(cat timetable.csv) gotmpl
39+
```
40+
3541
# Contributing
3642

3743
[Spatial Current, Inc.](https://spatialcurrent.io) is currently accepting pull requests for this repository. We'd love to have your contributions! Please see [Contributing.md](https://github.com/spatialcurrent/gotmpl/blob/master/CONTRIBUTING.md) for how to get started.

main.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ import (
1515
"text/template"
1616
)
1717

18+
import (
19+
"github.com/pkg/errors"
20+
)
21+
1822
import (
1923
"github.com/spatialcurrent/go-adaptive-functions/af"
24+
"github.com/spatialcurrent/go-simple-serializer/gss"
2025
)
2126

2227
func main() {
@@ -43,7 +48,33 @@ func main() {
4348
ctx[parts[0]] = parts[1]
4449
}
4550

46-
funcs := map[string]interface{}{}
51+
funcs := map[string]interface{}{
52+
"parse": func(args ...interface{}) (interface{}, error) {
53+
if len(args) != 2 {
54+
return nil, errors.New("invalid arguments for parse " + fmt.Sprint(args))
55+
}
56+
if text, ok := args[1].(string); ok {
57+
if f, ok := args[0].(string); ok {
58+
t, err := gss.GetType([]byte(text), f)
59+
if err != nil {
60+
return "", errors.Wrap(err, "error creating new object for format "+f)
61+
}
62+
return gss.DeserializeString(
63+
text,
64+
f,
65+
gss.NoHeader,
66+
gss.NoComment,
67+
true,
68+
0,
69+
gss.NoLimit,
70+
t,
71+
false,
72+
false)
73+
}
74+
}
75+
return nil, errors.New("invalid arguments for parse " + fmt.Sprint(args))
76+
},
77+
}
4778
for _, f := range af.Functions {
4879
f := f
4980
for _, alias := range f.Aliases {

0 commit comments

Comments
 (0)