builtin

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package builtin provides simple functions that can be used as global functions in a Scriggo template.

For example, to use the Min and Max functions as global min and max functions

globals := templates.Declarations{
    "min": builtin.Min,
    "max": builtin.Max,
}
opts := &templates.BuildOptions{
    Globals: globals,
}
template, err := scriggoTemplates.Build(fsys, file, opts)

And to use them in a template

{{ min(x, y) }}
{{ max(x, y) }}

Use this Declarations value to use all the builtin of this package in a template

templates.Declarations{
    "abbreviate":    builtin.Abbreviate,
    "abs":           builtin.Abs,
    "base64":        builtin.Base64,
    "capitalize":    builtin.Capitalize,
    "capitalizeAll": builtin.CapitalizeAll,
    "hasPrefix":     builtin.HasPrefix,
    "hasSuffix":     builtin.HasSuffix,
    "hex":           builtin.Hex,
    "hmacSHA1":      builtin.HmacSHA1,
    "hmacSHA256":    builtin.HmacSHA256,
    "htmlEscape":    builtin.HtmlEscape,
    "index":         builtin.Index,
    "indexAny":      builtin.IndexAny,
    "join":          builtin.Join,
    "lastIndex":     builtin.LastIndex,
    "max":           builtin.Max,
    "md5":           builtin.Md5,
    "min":           builtin.Min,
    "queryEscape":   builtin.QueryEscape,
    "replace":       builtin.Replace,
    "replaceAll":    builtin.ReplaceAll,
    "reverse":       builtin.Reverse,
    "runeCount":     builtin.RuneCount,
    "sha1":          builtin.Sha1,
    "sha256":        builtin.Sha256,
    "sort":          builtin.Sort,
    "split":         builtin.Split,
    "splitN":        builtin.SplitN,
    "sprint":        builtin.Sprint,
    "sprintf":       builtin.Sprintf,
    "toKebab":       builtin.ToKebab,
    "toLower":       builtin.ToLower,
    "toUpper":       builtin.ToUpper,
    "trim":          builtin.Trim,
    "trimLeft":      builtin.TrimLeft,
    "trimPrefix":    builtin.TrimPrefix,
    "trimRight":     builtin.TrimRight,
    "trimSuffix":    builtin.TrimSuffix,
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abbreviate

func Abbreviate(s string, n int) string

Abbreviate abbreviates s to almost n runes. If s is longer than n runes, the abbreviated string terminates with "...".

func Abs

func Abs(x int) int

Abs returns the absolute value of x.

func Base64

func Base64(s string) string

Base64 returns the base64 encoding of s.

func Capitalize

func Capitalize(src string) string

Capitalize returns a copy of the string src with the first non-separator in upper case.

func CapitalizeAll

func CapitalizeAll(src string) string

CapitalizeAll returns a copy of the string src with the first letter of each word in upper case.

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix tests whether the string s begins with prefix.

func HasSuffix

func HasSuffix(s, suffix string) bool

HasSuffix tests whether the string s ends with suffix.

func Hex

func Hex(src string) string

Hex returns the hexadecimal encoding of src.

func HmacSHA1

func HmacSHA1(message, key string) string

HmacSHA1 returns the HMAC-SHA1 tag for the given message and key, as a base64 encoded string.

func HmacSHA256

func HmacSHA256(message, key string) string

HmacSHA256 returns the HMAC-SHA256 tag for the given message and key, as a base64 encoded string.

func HtmlEscape

func HtmlEscape(s string) templates.HTML

HtmlEscape escapes s, replacing the characters <, >, &, " and ' and returns the escaped string as templates.HTML type.

func Index

func Index(s, substr string) int

Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.

func IndexAny

func IndexAny(s, chars string) int

IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.

func Join

func Join(elems []string, sep string) string

Join concatenates the elements of its first argument to create a single string. The separator string sep is placed between elements in the resulting string.

func LastIndex

func LastIndex(s, substr string) int

LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.

func Max

func Max(x, y int) int

Max returns the larger of x or y.

func Md5

func Md5(src string) string

Md5 returns the MD5 checksum of src as an hexadecimal encoded string.

func Min

func Min(x, y int) int

Min returns the smaller of x or y.

func QueryEscape

func QueryEscape(s string) string

QueryEscape escapes the string so it can be safely placed inside a URL query.

func Replace

func Replace(s, old, new string, n int) string

Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.

func ReplaceAll

func ReplaceAll(s, old, new string) string

ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.

func Reverse

func Reverse(data interface{})

Reverse returns the reverse order for data.

func RuneCount

func RuneCount(s string) (n int)

RuneCount returns the number of runes in s. Erroneous and short encodings are treated as single runes of width 1 byte.

func Sha1

func Sha1(src string) string

Sha1 returns the SHA1 checksum of src as an hexadecimal encoded string.

func Sha256

func Sha256(s string) string

Sha256 returns the SHA256 checksum of src as an hexadecimal encoded string.

func Sort

func Sort(slice interface{}, less func(i, j int) bool)

Sort sorts the provided slice given the provided less function.

The function panics if the provided interface is not a slice.

func Split

func Split(s, sep string) []string

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.

If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.

It is equivalent to SplitN with a count of -1.

func SplitN

func SplitN(s, sep string, n int) []string

SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.

The count determines the number of substrings to return:

n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
n < 0: all substrings

Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for Split.

func Sprint

func Sprint(a ...interface{}) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

func Sprintf

func Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string.

func ToKebab

func ToKebab(s string) string

ToKebab returns a copy of the string s in kebab case form.

func ToLower

func ToLower(s string) string

ToLower returns s with all Unicode letters mapped to their lower case.

func ToUpper

func ToUpper(s string) string

ToUpper returns s with all Unicode letters mapped to their upper case.

func Trim

func Trim(s, cutset string) string

Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.

func TrimLeft

func TrimLeft(s, cutset string) string

TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.

To remove a prefix, use TrimPrefix instead.

func TrimPrefix

func TrimPrefix(s, prefix string) string

TrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.

func TrimRight

func TrimRight(s, cutset string) string

TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.

To remove a suffix, use TrimSuffix instead.

func TrimSuffix

func TrimSuffix(s, suffix string) string

TrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.

Types

This section is empty.

Jump to

Keyboard shortcuts

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