funcs

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package funcs provides content-specific template functions for filtering and grouping.

Package funcs provides core template functions for arithmetic, logic, and comparisons.

Package funcs provides date and time formatting template functions.

Package funcs provides dictionary and data structure manipulation template functions.

Package funcs provides site-specific template functions requiring generator context.

Package funcs provides string manipulation template functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a, b any) (any, error)

func And

func And(values ...any) bool

func Append added in v0.0.38

func Append(slice []any, elem any) []any

Append adds an element to a slice and returns the new slice

func Concat added in v0.0.9

func Concat(slices ...[]*content.Node) []*content.Node

Concat merges multiple slices of nodes into one.

func Cond added in v0.0.36

func Cond(condition bool, trueVal, falseVal any) any

Cond is a ternary conditional operator: returns trueVal if condition is true, otherwise falseVal

func Contains

func Contains(haystack, needle any) (bool, error)

func Default

func Default(def, val any) any

Default returns the value if it's non-empty, otherwise returns the default. Now supports any type for both val and def (improved from string-only).

func Dict

func Dict(values ...any) (map[string]any, error)

Dict creates a map from key-value pairs.

func EndsWith added in v0.0.12

func EndsWith(s, suffix string) bool

EndsWith is an alias for HasSuffix.

func Eq

func Eq(a, b any) bool

func First

func First(items any) (any, error)

func FormatDate

func FormatDate(t time.Time, layout ...string) string

FormatDate formats a time.Time with an optional layout.

func Ge added in v0.0.36

func Ge(a, b any) (bool, error)

Ge returns true if a >= b

func Gt added in v0.0.36

func Gt(a, b any) (bool, error)

Gt returns true if a > b

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix tests whether a string begins with prefix.

func HasSuffix

func HasSuffix(s, suffix string) bool

HasSuffix tests whether a string ends with suffix.

func In added in v0.0.36

func In(needle, haystack any) (bool, error)

In is an alias for Contains with reversed argument order for more readable syntax. Usage: {{ if in "golang" .Tags }}...{{ end }} This is equivalent to: {{ if contains .Tags "golang" }}...{{ end }}

func Intersect added in v0.0.36

func Intersect(a, b []any) []any

Intersect returns elements that exist in both slices

func Join

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

Join joins a slice of strings with a separator.

func Last

func Last(items any) (any, error)

func Le added in v0.0.36

func Le(a, b any) (bool, error)

Le returns true if a <= b

func Len added in v0.0.36

func Len(v any) (int, error)

Len returns the length of a slice, array, map, or string

func Limit

func Limit(max any, items any) (any, error)

Limit returns at most n items from the collection.

func LoadAttribute

func LoadAttribute(path string) (template.HTML, error)

LoadAttribute loads file content as HTML-escaped attribute value.

func LoadData

func LoadData(path string) (template.HTML, error)

LoadData loads file content as HTML (unescaped).

func Lower

func Lower(s string) string

func Lt added in v0.0.36

func Lt(a, b any) (bool, error)

Lt returns true if a < b

func MakeSlice added in v0.1.1

func MakeSlice(items ...any) []any

MakeSlice creates a slice from the given arguments

func Merge added in v0.0.36

func Merge(dicts ...map[string]any) map[string]any

Merge merges multiple dictionaries into one. Later dictionaries override earlier ones.

func Ne

func Ne(a, b any) bool

func Now

func Now() time.Time

Now returns the current time.

func Or

func Or(values ...any) bool

func ParseDate

func ParseDate(layout, value string) (time.Time, error)

ParseDate parses a date string with the given layout.

func Pluralize

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

func Priority

func Priority(vals ...any) string
func RelatedPosts(pageData any, allPosts []*content.Node) []*content.Node

RelatedPosts finds posts related to the given page based on tag overlap.

func Replace

func Replace(s, old, replacement string) string

Replace replaces all occurrences of old with replacement in s.

func Reverse

func Reverse(items any) any

Reverse returns a copy of the slice with elements in reverse order. Supports both []*content.Node and generic []any.

func SafeHTML

func SafeHTML(s string) template.HTML

SafeHTML marks a string as safe HTML (bypasses auto-escaping).

func Slice added in v0.0.36

func Slice(v any, start int, end ...int) (any, error)

Slice returns a sub-slice of a slice, array, or string. Usage: {{ slice .Items 0 5 }}

func SortBy added in v0.0.9

func SortBy(field string, nodes []*content.Node) ([]*content.Node, error)

SortBy sorts a slice of nodes by a given field (only "date" currently supported).

func Split

func Split(s, sep string) []string

Split splits a string by a separator.

func StartsWith added in v0.0.12

func StartsWith(s, prefix string) bool

StartsWith is an alias for HasPrefix.

func Sub

func Sub(a, b int) int

func Trim

func Trim(s string) string

Trim removes leading and trailing whitespace.

func Union added in v0.0.36

func Union(a, b []any) []any

Union returns all unique elements from both slices

func Uniq added in v0.0.36

func Uniq(items []any) []any

Uniq removes duplicate elements from a slice, preserving order

func Upper

func Upper(s string) string

Upper converts a string to uppercase.

func Urlize

func Urlize(s string) string

Urlize converts a string to a URL-safe slug.

func Where

func Where(items any, field string, value any) (any, error)

Where filters a slice of items by a field value. Supports both map[string]any and *content.Node.

Types

type Group

type Group struct {
	Key   string
	Items []*content.Node
}

Group represents a grouped collection of content nodes.

func GroupBy

func GroupBy(criteria string, nodes []*content.Node) ([]Group, error)

GroupBy groups content nodes by a given criteria (year, month, day).

type MacroRenderer

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

MacroRenderer holds methods for rendering template macros.

func NewMacroRenderer

func NewMacroRenderer(templates *template.Template) *MacroRenderer

NewMacroRenderer creates a new macro renderer.

func (*MacroRenderer) RenderPagination

func (mr *MacroRenderer) RenderPagination(siteConfig map[string]any) func(data map[string]any) (template.HTML, error)

RenderPagination renders the pagination macro.

type SiteContext

type SiteContext struct {
	BaseURL    string
	ContentMap map[string]*content.Node
	Markdown   goldmark.Markdown
	I18n       *i18n.I18n
}

SiteContext provides access to site-wide data needed by template functions.

type SiteFuncs

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

SiteFuncs holds template functions that require site context.

func NewSiteFuncs

func NewSiteFuncs(ctx *SiteContext) *SiteFuncs

NewSiteFuncs creates a new SiteFuncs with the given context.

func (*SiteFuncs) AssetPath

func (sf *SiteFuncs) AssetPath(relPath string, context any) (string, error)

AssetPath generates the full URL for an asset path.

func (*SiteFuncs) CurrentLang added in v0.0.33

func (sf *SiteFuncs) CurrentLang(context any) string

CurrentLang returns the current language code from context. Usage in templates: {{ currentLang .Page }}

func (*SiteFuncs) GetSection

func (sf *SiteFuncs) GetSection(path string) (*content.Node, error)

GetSection retrieves a section node by path.

func (*SiteFuncs) LangURL added in v0.0.33

func (sf *SiteFuncs) LangURL(langCode string, pageOrPath any) string

LangURL generates a URL for a specific language version of a page. Usage in templates:

{{ langURL "fr" .Page }}           - Get current page in French
{{ langURL "en" .Page.Permalink }} - Get specific path in English

func (*SiteFuncs) RenderMarkdown

func (sf *SiteFuncs) RenderMarkdown(input string) (template.HTML, error)

RenderMarkdown renders markdown content to HTML.

func (*SiteFuncs) Translate added in v0.0.33

func (sf *SiteFuncs) Translate(key string, langOrContext ...any) (string, error)

Translate returns a translation for the given key in the specified language. Usage in templates:

{{ i18n "section.contact" }}  - uses current page language from context
{{ i18n "hello" "fr" }}       - explicit language

Jump to

Keyboard shortcuts

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