theme

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 20 Imported by: 2 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Theme

Installation

go get -u github.com/gowool/theme

License

Distributed under MIT License, please see license file within the code for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NewChainLoader      = et.NewChainLoader
	NewMemoryLoader     = et.NewMemoryLoader
	NewFileSystemLoader = et.NewFileSystemLoader
	NewFSLoaderWithNS   = et.NewFSLoaderWithNS
)
View Source
var Funcs = template.FuncMap{
	"ternary": func(condition bool, trueValue, falseValue any) any {
		if condition {
			return trueValue
		}
		return falseValue
	},
	"js":        func(str string) template.JS { return template.JS(str) },
	"css":       func(str string) template.CSS { return template.CSS(str) },
	"raw":       func(s string) template.HTML { return template.HTML(s) },
	"html":      func(str string) template.HTML { return template.HTML(str) },
	"html_attr": func(str string) template.HTMLAttr { return template.HTMLAttr(str) },
	"empty": func(given any) bool {
		g := reflect.ValueOf(given)
		return !g.IsValid() || g.IsNil() || g.IsZero()
	},
	"escape": html.EscapeString,
	"deref": func(s any) any {
		v := reflect.ValueOf(s)
		if v.Kind() == reflect.Pointer {
			return v.Elem().Interface()
		}
		return s
	},
	"dump": spew.Sdump,
	"str_build": func(str ...string) string {
		var b strings.Builder
		for _, s := range str {
			b.WriteString(s)
		}
		return b.String()
	},
	"str_camelcase":  camelcase.Camelcase,
	"str_snakecase":  snakecase.Snakecase,
	"str_trim":       strings.TrimSpace,
	"str_trim_left":  strings.TrimPrefix,
	"str_trim_right": strings.TrimSuffix,
	"str_upper":      strings.ToUpper,
	"str_lower":      strings.ToLower,
	"str_title":      strings.ToTitle,
	"str_contains":   strings.Contains,
	"str_has_prefix": strings.HasPrefix,
	"str_has_suffix": strings.HasSuffix,
	"str_replace":    strings.ReplaceAll,
	"str_equal":      strings.EqualFold,
	"str_index":      strings.Index,
	"str_join":       strings.Join,
	"str_split":      strings.Split,
	"str_split_n":    strings.SplitN,
	"str_fields":     strings.Fields,
	"str_repeat":     strings.Repeat,
	"str_len":        func(s string) int { return utf8.RuneCountInString(s) },
	"json": func(v any) string {
		return Encode(v, json.Marshal)
	},
	"xml": func(v any) string {
		return Encode(v, xml.Marshal)
	},
	"yaml": func(v any) string {
		return Encode(v, yaml.Marshal)
	},
	"json_pretty": func(v any) string {
		return Pretty(v, json.MarshalIndent)
	},
	"xml_pretty": func(v any) string {
		return Pretty(v, xml.MarshalIndent)
	},
	"yaml_pretty": func(v any) string {
		var buf bytes.Buffer
		enc := yaml.NewEncoder(&buf)
		enc.SetIndent(2)
		if err := enc.Encode(v); err != nil {
			return ""
		}
		return template.JSEscapeString(internal.String(buf.Bytes()))
	},
	"seq":   Seq,
	"len":   func(v any) int { return reflect.ValueOf(v).Len() },
	"list":  func(v ...any) []any { return v },
	"at":    func(v []any, i int) any { return v[i] },
	"first": func(v []any) any { return v[0] },
	"last":  func(v []any) any { return v[len(v)-1] },
	"slice": func(v []any, indices ...int) []any {
		switch len(indices) {
		case 0:
			return v[:]
		case 1:
			return v[indices[0]:]
		default:
			return v[indices[0]:indices[1]]
		}
	},
	"append":   func(v []any, e ...any) []any { return append(v, e...) },
	"prepend":  func(v []any, e ...any) []any { return append(e, v...) },
	"reverse":  func(v []any) []any { slices.Reverse(v); return v },
	"repeat":   func(v []any, count int) []any { return slices.Repeat(v, count) },
	"contains": func(v []any, i any) bool { return slices.Contains(v, i) },
	"index_of": func(v []any, i any) int { return slices.Index(v, i) },
	"concat":   func(sl ...[]any) []any { return slices.Concat(sl...) },
	"dict": func(v ...any) map[any]any {
		if len(v)%2 != 0 {
			v = append(v, "")
		}
		dict := make(map[any]any, len(v)/2)
		for i := 0; i < len(v); i += 2 {
			dict[v[i]] = v[i+1]
		}
		return dict
	},
	"keys":   func(m map[any]any) []any { return maps.Keys(m) },
	"values": func(m map[any]any) []any { return maps.Values(m) },
	"has":    func(m map[any]any, k any) bool { _, ok := m[k]; return ok },
	"get":    func(m map[any]any, k any) any { return m[k] },
	"set":    func(m map[any]any, k, v any) map[any]any { m[k] = v; return m },
	"unset":  func(m map[any]any, k any) map[any]any { delete(m, k); return m },
	"now":    time.Now,
	"date":   FormatDate,
	"date_local": func(fmt string, date any) string {
		return FormatDate(fmt, date, "Local")
	},
	"date_utc": func(fmt string, date any) string {
		return FormatDate(fmt, date, "UTC")
	},
}

Functions

func Encode added in v1.1.0

func Encode(v any, fn func(v any) ([]byte, error)) string

func FormatDate added in v1.1.0

func FormatDate(fmt string, date any, location string) string

func Pretty added in v1.1.0

func Pretty(v any, fn func(v any, prefix, indent string) ([]byte, error)) string

func Seq added in v1.1.0

func Seq(args ...int) []int

Seq creates a sequence of integers from args.

Examples:

3 => 1, 2, 3
1 2 4 => 1, 3
-3 => -1, -2, -3
1 4 => 1, 2, 3, 4
1 -2 => 1, 0, -1, -2

Types

type ChainLoader

type ChainLoader = et.ChainLoader

type FileSystemLoader

type FileSystemLoader = et.FileSystemLoader

type FuncMap

type FuncMap func(t Theme) template.FuncMap

type Handler

type Handler = et.Handler

type Loader

type Loader = et.Loader

type MemoryLoader

type MemoryLoader = et.MemoryLoader

type Repository

type Repository interface {
	FindByName(ctx context.Context, name string) (Template, error)
}

type RepositoryLoader

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

func NewRepositoryLoader

func NewRepositoryLoader(repository Repository) *RepositoryLoader

func (*RepositoryLoader) Exists

func (l *RepositoryLoader) Exists(ctx context.Context, name string) (bool, error)

func (*RepositoryLoader) Get

func (l *RepositoryLoader) Get(ctx context.Context, name string) (*Source, error)

func (*RepositoryLoader) IsFresh

func (l *RepositoryLoader) IsFresh(ctx context.Context, name string, t int64) (bool, error)

type Source

type Source = et.Source

type Template

type Template interface {
	Code() []byte
	Changed() time.Time
}

type Theme

type Theme interface {
	Debug(debug bool) Theme
	Funcs(funcMap FuncMap) Theme
	Global(global ...string) Theme
	Write(ctx context.Context, w io.Writer, name string, data any) error
	HTML(ctx context.Context, name string, data any) (string, error)
}

func New

func New(loader Loader, handlers ...Handler) Theme

Directories

Path Synopsis
fx module

Jump to

Keyboard shortcuts

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