bilit

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2020 License: MIT Imports: 3 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Bilit

MIT Licensed Latest Version

Bilit (pronounced /ˈbilit/) is a bidirectional template library for Go. Inspired by Javascript's Template Literals and Nunjucks

Powered by Regular Expressions

Examples

Modern API - Basic:

Open Example Folder Try it on Go Playground

template := bilit.Template("Hello, I'm {{name}} from ${City}, {{from_state}}")

dataSrc := map[string]string{
	"name":       "John",
	"City":       "Dallas",
	"from_state": "TX",
}

str := template.Populate(dataSrc)
fmt.Println(str, "end")
// "Hello, I'm John from Dallas, TX"

data := template.Pull(tr)
fmt.Println(data)
// map[City:Dallas from_state:TX name:John]
Function API (Old API) - Basic:

Open Example Folder Try it on Go Playground

const template = "Hello, I'm {{name}} from ${City}, {{from_state}}"

dataSrc := map[string]string{
	"name":       "John",
	"City":       "Dallas",
	"from_state": "TX",
}

str := bilit.Populate(template, dataSrc)
fmt.Println(str, "end")
// "Hello, I'm John from Dallas, TX"

data := bilit.Pull(template, str)
fmt.Println(data)
// map[City:Dallas from_state:TX name:John]

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultPattern is the default pattern for the regex
	DefaultPattern = ".+"

	// Debug enables printing of the map passed to a template
	Debug = false
	// Unsafe disables RegExp escaping
	Unsafe = false
)
View Source
var (
	// GroupRegexp extracts the name of a nunjucks style lookup
	GroupRegexp = regexp.MustCompile(
		`(?m)[${]{2}(?P<groupName>[^{}$]*)[}]{1,2}`,
	)

	// BeforeEscape escapes characters - Before Groups
	BeforeEscape = regexp.MustCompile(
		`(?m)[\[\]().+?^]`,
	)

	// QuantityEscape escape quantity specifiers - After Groups
	QuantityEscape = regexp.MustCompile(
		`{(.*)}`,
	)
	// AfterEscape escapes characters - After Groups
	AfterEscape = regexp.MustCompile(`[$]`)
)

Functions

func Populate

func Populate(template string, data map[string]string) string

Populate for old api compatibility

func Pull

func Pull(template, str string) map[string]string

Pull for old api compatibility

Types

type Tmpl

type Tmpl struct {
	// contains filtered or unexported fields
}

Tmpl is a bilit template

func Template

func Template(str string, pattern ...string) Tmpl

Template makes a template

func (Tmpl) Populate

func (t Tmpl) Populate(data map[string]string) string

Populate populates a template with data

Example
Debug = true
tmpl := Template("Hello, I'm {{name}} from ${City}, {{from_state}}")
str := tmpl.Populate(map[string]string{
	"name":       "John",
	"City":       "Dallas",
	"from_state": "TX",
})
fmt.Print(str)
Output:
Hello, I'm John from Dallas, TX

func (Tmpl) Pull

func (t Tmpl) Pull(str string) map[string]string

Pull extracts data from a populated string using a template

Example
Debug = true
tmpl := Template("Hello, I'm {{name}} from ${City}, {{from_state}}")
str := tmpl.Pull("Hello, I'm John from Dallas, TX")
fmt.Print(str)
Output:
map[City:Dallas from_state:TX name:John]

func (Tmpl) RegEx

func (t Tmpl) RegEx() *regexp.Regexp

RegEx makes the pull regular expression

Directories

Path Synopsis
examples
function module
modern module

Jump to

Keyboard shortcuts

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