Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chai2010/template
template helper
https://github.com/chai2010/template
Last synced: 3 months ago
JSON representation
template helper
- Host: GitHub
- URL: https://github.com/chai2010/template
- Owner: chai2010
- License: bsd-3-clause
- Created: 2019-01-22T07:27:49.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-05T09:48:45.000Z (over 4 years ago)
- Last Synced: 2024-10-06T01:18:20.063Z (3 months ago)
- Language: Go
- Homepage: https://godoc.org/github.com/chai2010/template
- Size: 22.5 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
- *Go语言QQ群: 102319854, 1055927514*
- *凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa*----
# tmplate helper
[![Build Status](https://travis-ci.org/chai2010/template.svg)](https://travis-ci.org/chai2010/template)
[![Go Report Card](https://goreportcard.com/badge/github.com/chai2010/template)](https://goreportcard.com/report/github.com/chai2010/template)
[![GoDoc](https://godoc.org/github.com/chai2010/template?status.svg)](https://godoc.org/github.com/chai2010/template)
[![License](http://img.shields.io/badge/license-BSD-blue.svg)](https://github.com/chai2010/template/blob/master/LICENSE)## Example
```go
package mainimport (
"fmt"
"image"
"strings""github.com/chai2010/template"
)func main() {
fmt.Println(
template.MustRender(`Hello, {{.}}`, "Neo"),
)
fmt.Println(
template.MustRender(`Hello, {{index . 0}}`, []string{"Go"}),
)
fmt.Println(
template.MustRender(`Hello, {{index . "Name" "SubName"}}`,
map[string]interface{}{
"Name": map[string]string {
"SubName": "凹(Wa)",
},
},
),
)fmt.Println(
template.MustRender(`Hello, {{.Name}}`, map[string]string{
"Name": "Lua",
}),
)
fmt.Println(
template.MustRender(`Hello, {{.Name}}`, struct{ Name string }{
Name: "Ruby",
}),
)fmt.Println(
template.MustRender(
`Hello, {{upper .Name}}, Age: {{Age}}; Point: {{Point.X}}, {{Map.Name.SubName}}`,
struct{ Name string }{
Name: "chai2010",
},
template.FuncMap{
"upper": strings.ToUpper,
"Age": func() int { return 20 },
"Point": func() image.Point { return image.Pt(123, 456) },
"Map": func() map[string]interface{} {
return map[string]interface{}{
"Name": struct{ SubName string }{
SubName: "chai2010",
},
}
},
},
),
)fmt.Println(
template.MustRender(
`{{range $i, $v := .}}{{$v.Book}}{{end}}`,
[]struct{ Name, Book string }{
{Name: "chai2010", Book: "《Go语言高级编程》"},
{Name: "chai2010 & ending", Book: "《WebAssembly标准入门》"},
{Name: "ending & chai2010", Book: "《C/C++面向WebAssembly编程》"},
},
),
)fmt.Println(
template.MustRender(
`{{range .}}<{{.}}>{{end}}`, []string{"hello", "world"},
),
)fmt.Println(
template.MustRenderWithDelims(`Hello, {{<<.))}}`, `<<`, `))`, "Neo"),
)fmt.Println(
template.MustRender(
`{{.}}: {{A}}-{{B}}-{{C}}, {{if A}}if A == true{{end}}`,
"Self", template.FuncMap{
"A": func() bool { return true },
"B": func() int { return 9527 },
"C": func() string { return "C-Value" },
},
),
)fmt.Println(
template.MustRender(
`slice: {{range $i, $v := slice}}{{$i}}:{{$v}} {{end}}`, nil,
template.FuncMap{
"slice": func() []string {
return []string{"A", "B", "C"}
},
},
),
)fmt.Println(
template.MustRenderFile("hello.tmpl", map[string]string{"Name": "World"}),
)// Output:
// Hello, Neo
// Hello, Go
// Hello, 凹(Wa)
// Hello, Lua
// Hello, Ruby
// Hello, CHAI2010, Age: 20; Point: 123, chai2010
// 《Go语言高级编程》《WebAssembly标准入门》《C/C++面向WebAssembly编程》
//
// Hello, {{Neo}}
// Self: true-9527-C-Value, if A == true
// slice: 0:A 1:B 2:C
// Hello, World
}
```## BUGS
Report bugs to .
Thanks!