Golang template engine with Js syntax !!
github.com/CoinSummer/go-template@v0.2.0
- Custom operator and functions
- Interpolate from env
- Expressions only, easy to use
package main
import (
"fmt"
gt "github.com/CoinSummer/go-template"
)
func main() {
tp := gt.NewTemplate("Everyone knows {$a.b + $a.b} == { round($a.b + $a.b, 1)}", nil)
res, err := tp.Render( `{"a": {"b": 1}}`)
if err != nil {
panic(err)
}
fmt.Println(res) // Everyone knows 2 == 2
}
package main
import (
"fmt"
gt "github.com/CoinSummer/go-template"
)
func main() {
tp := gt.NewTemplateWithConfig("Everyone knows {$a.b + $a.b} == { round($a.b + $a.b, 1)}", nil, >.TemplateConfig{
TimeOffset: 8,
TimeFormat: time.RFC3339,
})
res, err := tp.Render( `{"a": {"b": 1}}`)
if err != nil {
panic(err)
}
fmt.Println(res) // Everyone knows 2 == 2
}
package main
import (
"fmt"
gt "github.com/CoinSummer/go-template"
"github.com/shopspring/decimal"
)
func main() {
engine := gt.NewTemplateEngine()
engine.OperatorsMgr.RegisterFunc("%", func(arg1, arg2 interface{}) (interface{}, error) {
a, ok := arg1.(decimal.Decimal)
if !ok {
return nil, fmt.Errorf("%% with NaN: %v", a)
}
b, ok := arg2.(decimal.Decimal)
if !ok {
return nil, fmt.Errorf("%% to NaN: %v", b)
}
return a.Mod(b), nil
})
tp := gt.NewTemplate("100 % 3 = {100 % 3}", engine)
res, err := tp.Render(``)
if err != nil {
panic(err)
}
fmt.Println(res)
}