tplutil

package module
v0.0.0-...-f880f62 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: Apache-2.0 Imports: 7 Imported by: 7

README

Documentation

Overview

Package tplutil provides more convient way to use text/template inside the code.

Consider (using text/template):

var myTpl = template.Must(template.New("name").Parse(
	"Some list:\n" +
		"{{range .}}" +
		"# {{.}}\n" +
		"{{end}}"))

`gofmt` will ruin any attempt to format code above.

And with tplutil:

var myTpl = template.Must(template.New("name").Parse(tplutil.Strip(`
	Some list:{{"\n"}}

	{{range .}}
		# {{.}}{{"\n"}}
	{{end}}
`)))

Output will be exactly the same.

Any indenting whitespaces and newlines will be ignored. If must, they should be specified by using syntax

`{{" "}}` or `{{"\n"}}`.

It also provide `{{last}}` function to check on last element of pipeline:

var myTpl = template.Must(template.New("asd").Funcs(tplutil.Last).Parse(
	tplutil.Strip(`
		Some list:{{"\n"}}

		{{range $i, $_ := .}}
			{{.}}
			{{if not (last $i $)}}
				{{"\n"}}{{/* do not append newline to the last element */}}
			{{end}}
		{{end}}
	`))

Index

Constants

This section is empty.

Variables

View Source
var Last = template.FuncMap{
	"last": func(x int, a interface{}) bool {
		return x == reflect.ValueOf(a).Len()-1
	},
}

Functions

func ExecuteToString

func ExecuteToString(tpl *template.Template, v interface{}) (string, error)

ExecuteToString applies a parsed template to specified data object and returns it output as return value. It can return partial result if execution can'tpl be proceed because of error.

func ParseGlob

func ParseGlob(tpl *template.Template, pattern string) (
	*template.Template, error,
)

ParseGlob do the same as template.ParseGlob(), but will allow to use sparse syntax (like in examples above) in files.

func Strip

func Strip(text string) string

Types

This section is empty.

Jump to

Keyboard shortcuts

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