got

package module
v0.0.0-...-4fbdec7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2019 License: MIT Imports: 3 Imported by: 0

README

Got

GoDoc

a very simple Go's struct template parser

Usage

  • Install
$ go get github.com/Bhinneka/got
  • Parse Template to stdio

you can create a custom function inside your template:

	funcMaps := template.FuncMap{
		"toUpper": func(v string) string {
			return strings.ToUpper(v)
		},
	}

example:

// Person struct
type Person struct {
	ID   string
	Name string
}

func main() {

	data := Person{
		ID:   "1",
		Name: "Wuriyanto",
	}

	tmpl := `
	Hello
	Your ID : {{ .ID }}
	Your Name : {{ toUpper .Name }}
	`

	funcMaps := template.FuncMap{
		"toUpper": func(v string) string {
			return strings.ToUpper(v)
		},
	}

	err := got.Parse(tmpl, data, os.Stdout, funcMaps, got.ParseTemplateText)

	if err != nil {
		fmt.Println(err)
    }
    
    //output :

    //Hello
    //Your ID : 1
    //Your Name : WURIYANTO

}
  • Parse template file my_template.tmpl
<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <h1>{{ .OrderID }}</h1>
        <ul>
            {{- range $item := .Items}}
                <li>{{ $item }}</li>
            {{- end }}
        </ul>
    </body>
</html>

Order.go struct

// Order struct
type Order struct {
	OrderID string
	Items   []string
}

Parse this go struct to my_template.tmpl file

func main() {
	myOrders := Order{
		OrderID: "00881119977",
		Items:   []string{"Coffee mix", "Tea", "Mendoan", "Gorengan", "Onde-onde"},
	}

	var target strings.Builder

	err := got.Parse("my_template.tmpl", myOrders, &target, nil, got.ParseTemplateFile)

	if err != nil {
		fmt.Println(err)
	}

    fmt.Println(target.String())
    
    //output:

    // <html>
    //     <head>
    //         <title>Example</title>
    //     </head>
    //     <body>
    //         <h1>00881119977</h1>
    //         <ul>
    //                 <li>Coffee mix</li>
    //                 <li>Tea</li>
    //                 <li>Mendoan</li>
    //                 <li>Gorengan</li>
    //                 <li>Onde-onde</li>
    //         </ul>
    //     </body>
    // </html>
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(input string, data interface{}, target io.Writer, funcMaps template.FuncMap, f ParserTemplateFunc) error

Parse will parse specific input file to io.Writer you can put anything to target parameter, as long as target is io.Writer implementation

func ParseTemplateFile

func ParseTemplateFile(filePath string, data interface{}, funcMaps template.FuncMap, result io.Writer) error

ParseTemplateFile function will parse template from file

func ParseTemplateText

func ParseTemplateText(text string, data interface{}, funcMaps template.FuncMap, result io.Writer) error

ParseTemplateText function will parse template from text

Types

type ParserTemplateFunc

type ParserTemplateFunc func(string, interface{}, template.FuncMap, io.Writer) error

ParserTemplateFunc type of parser function

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL