theme

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 20 Imported by: 9

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{
	"empty": func(given any) bool {
		g := reflect.ValueOf(given)
		return !g.IsValid() || g.IsNil() || g.IsZero()
	},
	"raw": func(s string) template.HTML {
		return template.HTML(s)
	},
	"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_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()))
	},
	"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":    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

This section is empty.

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