Categorygithub.com/CoinSummer/go-template
modulepackage
1.4.6
Repository: https://github.com/coinsummer/go-template.git
Documentation: pkg.go.dev

# README

Golang template engine with Js syntax !!

Dependency

github.com/CoinSummer/[email protected]

Features

  1. Custom operator and functions
  2. Interpolate from env
  3. Expressions only, easy to use

Usage

Hello world

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
}

With config

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, &gt.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
}

Custom operator

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) 
}

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author