beehive: github.com/muesli/beehive/templatehelper Index | Files

package templatehelper

import "github.com/muesli/beehive/templatehelper"

Package templatehelper provides a func-map of common template functions

Index

Package Files

templatehelper.go

Variables

var (
    FuncMap = template.FuncMap{
        "JSON": func(values ...interface{}) htmlTemplate.JS {
            json, _ := json.Marshal(values)
            return htmlTemplate.JS(json)
        },
        "Left": func(s string, n int) string {
            if n > len(s) {
                n = len(s)
            }
            return s[:n]
        },
        "Matches": func(s string, pattern string) (bool, error) {
            return regexp.MatchString(pattern, s)
        },
        "Mid": func(s string, left int, values ...int) string {
            if left > len(s) {
                left = len(s)
            }

            if len(values) != 0 {
                right := values[0]
                if right > len(s) {
                    right = len(s)
                }
                return s[left:right]
            }
            return s[left:]
        },
        "Right": func(s string, right int) string {
            left := len(s) - right
            if left < 0 {
                left = 0
            }
            return s[left:]
        },
        "Last": func(items []string) (string, error) {
            if len(items) == 0 {
                return "", errors.New("cannot get last element from empty slice")
            }
            return items[len(items)-1], nil
        },

        "Compare":      strings.Compare,
        "Contains":     strings.Contains,
        "ContainsAny":  strings.ContainsAny,
        "Count":        strings.Count,
        "EqualFold":    strings.EqualFold,
        "HasPrefix":    strings.HasPrefix,
        "HasSuffix":    strings.HasSuffix,
        "Index":        strings.Index,
        "IndexAny":     strings.IndexAny,
        "Join":         strings.Join,
        "LastIndex":    strings.LastIndex,
        "LastIndexAny": strings.LastIndexAny,
        "Repeat":       strings.Repeat,
        "Replace":      strings.Replace,
        "Split":        strings.Split,
        "SplitAfter":   strings.SplitAfter,
        "SplitAfterN":  strings.SplitAfterN,
        "SplitN":       strings.SplitN,
        "TimeNow":      time.Now,
        "Title":        strings.Title,
        "ToLower":      strings.ToLower,
        "ToTitle":      strings.ToTitle,
        "ToUpper":      strings.ToUpper,
        "Trim":         strings.Trim,
        "TrimLeft":     strings.TrimLeft,
        "TrimPrefix":   strings.TrimPrefix,
        "TrimRight":    strings.TrimRight,
        "TrimSpace":    strings.TrimSpace,
        "TrimSuffix":   strings.TrimSuffix,
    }
)

FuncMap contains a few convenient template helpers

Package templatehelper imports 7 packages (graph) and is imported by 28 packages. Updated 2020-12-17. Refresh now. Tools for package owners.