funcs

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FuncMap = template.FuncMap{

	"yesno": YesNo,

	"inputAttrs": InputAttrs,

	"safeHTML": safeHTML,
	"safeAttr": safeAttr,
	"safeCSS":  safeCSS,
	"safeJS":   safeJS,
	"safeURL":  safeURL,

	"classMap": ClassMap,

	"isEven": isEven,
	"isOdd":  isOdd,

	"int": toInt64,

	"slice": slice,

	"contains":   strings.Contains,
	"hasPrefix":  strings.HasPrefix,
	"hasSuffix":  strings.HasSuffix,
	"humanize":   Humanize,
	"isBlank":    IsBlank,
	"join":       strings.Join,
	"lower":      strings.ToLower,
	"notBlank":   NotBlank,
	"pluralize":  Pluralize,
	"replaceAll": strings.ReplaceAll,
	"replace":    strings.Replace,
	"slugify":    Slugify,
	"split":      strings.Split,
	"trim":       strings.TrimSpace,
	"trimPrefix": strings.TrimPrefix,
	"trimSuffix": strings.TrimSuffix,
	"truncate":   Truncate,
	"upper":      strings.ToUpper,

	"now":   time.Now,
	"since": time.Since,
	"until": time.Until,
}

Functions

func ApproximateDuration

func ApproximateDuration(d time.Duration) string

func ClassMap

func ClassMap(classMap ...any) (string, error)

ClassMap takes a pair of classes and boolean expressions, and returns a single string with the active classes.

func FormatDuration

func FormatDuration(d time.Duration) string

func FormatTime

func FormatTime(t time.Time, format string) string

func Humanize

func Humanize(s string) string

Humanize a string from snake case, slug, or camel case to a human readable string. Example: "hello_world" -> "Hello world"

func InputAttrs

func InputAttrs(nameID string, attrs ...any) (map[string]any, error)

InputAttrs prepares the data for rendering an input field in a separate template. nameID is used for both the name and ID attributes for simplicity. errors is a slice of strings representing validation messages for this input. attrs are additional attributes provided as key/value pairs. Use a "label" attribute to set the label text, and a "type" attribute to set the input type. Use a "hint" attribute to set the hint text. Use an "error" attribute to set an error message. Use a "togglePassword" attribute to "true" to add a toggle button for password visibility (only for password inputs).

func IsBlank

func IsBlank(s string) bool

func NotBlank

func NotBlank(s string) bool

func Pluralize

func Pluralize(count any, singular string, plural string) (string, error)

func Slugify

func Slugify(s string) string

Slugify converts a string to a slug naively. For a more robust solution, consider using a library like github.com/gosimple/slug

func Srcset

func Srcset(src string, widths ...string) string

Srcset returns a srcset string for an image with the given src and widths. It takes a src string and a variadic list of widths. The widths can be either a number or a number followed by a "w" or "x" suffix. If the width does not have a suffix, it will default to "w". Use "x" for density descriptors. Example:

Srcset("/foo/example.webp", "100", "200", "300")
// => "/foo/example-100w.webp, /foo/example-200w.webp, /foo/example-300w.webp"
Srcset("/foo/example.webp", "100w", "200w", "300w")
// => "/foo/example-100w.webp, /foo/example-200w.webp, /foo/example-300w.webp"
Srcset("/example.jpg", "1x", "2x")
// => "/example-1x.jpg, /example-2x.jpg"

func Truncate

func Truncate(s string, n int) string

func YesNo

func YesNo(b bool) string

Types

This section is empty.

Jump to

Keyboard shortcuts

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