funtemplates

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 4 Imported by: 0

README

Go Template Function Operations

The goal of this library is to quickly add some reflection based functional functions to the text/template and html/template language. Namely:

  • map
  • filter
  • find
  • findIndex

This library exists in lieu of generic support in text/template or html/template.

Exported functions:

  • func MapTemplateFunc(slice any, f any) (any, error) The map function.
  • func FilterTemplateFunc(slice any, f any) (any, error) The map function.
  • func TextFunctions() text/template.FuncMap
  • func HtmlFunctions() html/template.FuncMap

Template Function definitions

interface.go:

func TextFunctions() tt.FuncMap {
	return map[string]any{
		"filter":    FilterTemplateFunc,
		"find":      FindTemplateFunc,
		"findIndex": FindIndexTemplateFunc,
		"map":       MapTemplateFunc,
	}
}

func HtmlFunctions() ht.FuncMap {
	return map[string]any{
		"filter":    FilterTemplateFunc,
		"find":      FindTemplateFunc,
		"findIndex": FindIndexTemplateFunc,
		"map":       MapTemplateFunc,
	}
}

map

In go: MapTemplateFunc, provided as map by TextFunctions and HtmlFunctions

Definition:

func MapTemplateFunc(slice any, f any) (any, error)
  • The first argument slice must be a slice (or nil), of any type.
  • Second argument f must be a function of these definitions:
    • func () any
    • func () (any, error)
    • func (v any) any
    • func (v any) (any, error)

The return will be:

  • The first result: an array of the same length as slice in the case, or nil if there was an error. The output of f will be in the appropriate place for each value
  • The 2nd result: an error if there was an error: See errors.go for a complete list.

Usage:

  • {{ map $.Data $.Funcs.inc }}
  • {{ map $.Data $.Funcs.odd }}

filter

In go: FilterTemplateFunc, provided as filter by TextFunctions and HtmlFunctions

Definition:

func FilterTemplateFunc(slice any, f any) (any, error)
  • The first argument slice must be a slice (or nil), of any type.
  • Second argument f must be a function of these definitions:
    • func () bool
    • func () (bool, error)
    • func (v any) bool
    • func (v any) (bool, error)

The return will be:

  • The first result: an array of the same length or smaller as slice in the case, or nil if there was an error. The output of f will be a slice which only contains the values which f returned true for slice
  • The 2nd result: an error if there was an error: See errors.go for a complete list.

Usage:

  • {{ filter $.Data $.Funcs.odd }}

find

In go: FindTemplateFunc, provided as find by TextFunctions and HtmlFunctions

Definition:

func FindTemplateFunc(slice any, f any) (any, error)
  • The first argument slice must be a slice (or nil), of any type.
  • Second argument f must be a function of these definitions:
    • func () bool
    • func () (bool, error)
    • func (v any) bool
    • func (v any) (bool, error)

The return will be:

  • The first matching value, or nil
  • The 2nd result: an error if there was an error: See errors.go for a complete list.

Usage:

  • {{ filter $.Data $.Funcs.odd }}

findIndex

In go: FindIndexTemplateFunc, provided as findIndex by TextFunctions and HtmlFunctions

Definition:

func FindIndexTemplateFunc(slice any, f any) (int, error)
  • The first argument slice must be a slice (or nil), of any type.
  • Second argument f must be a function of these definitions:
    • func () bool
    • func () (bool, error)
    • func (v any) bool
    • func (v any) (bool, error)

The return will be:

  • The first matching index, or -1
  • The 2nd result: an error if there was an error: See errors.go for a complete list.

Usage:

  • {{ filter $.Data $.Funcs.odd }}

Usage:

package main

import (
	"fmt"
	"github.com/arran4/go-template-functional-operations"
	"github.com/arran4/go-template-functional-operations/misc"
	"os"
	"text/template"
)

func main() {
	funcs := map[string]any{
		"inc": func(i int) int {
			return i + 1
		},
		"odd": func(i int) bool {
			return i%2 == 1
		},
	}
	funcs = misc.MergeMaps(funtemplates.TextFunctions(), funcs)

	tmpl := template.Must(template.New("").Funcs(funcs).Parse(`
        {{ map $.Data $.Funcs.inc }}
        {{ map $.Data $.Funcs.odd }}
    `))

	data := struct {
		Data  []int
		Funcs map[string]any
	}{
		Data:  []int{1, 2, 3, 4},
		Funcs: funcs,
	}

	err := tmpl.Execute(os.Stdout, data)
	if err != nil {
		fmt.Println(err)
	}
}

Returns:


        [2 3 4 5]
        [true false true false]
    

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInputFuncMustTake0or1Arguments  = errors.New("expected second parameter function to take 0 or 1 parameters")
	ErrExpectedFirstParameterToBeSlice = errors.New("expected first parameter to be an slice")
	ErrExpected2ndArgumentToBeFunction = errors.New("expected second parameter to be a function")
	ErrExpectedSecondReturnToBeError   = errors.New("expected second return type to be assignable to error")
	ErrExpected1Or2ReturnTypes         = errors.New("expected return with 1 or 2 arguments of types (any, error?)")
	ErrExpectedFirstReturnToBeBool     = errors.New("expected first return type to be assignable to bool")
)

Functions

func FilterTemplateFunc

func FilterTemplateFunc(slice any, f any) (any, error)

func FindIndexTemplateFunc

func FindIndexTemplateFunc(slice any, f any) (int, error)

func FindTemplateFunc

func FindTemplateFunc(slice any, f any) (any, error)

func HtmlFunctions

func HtmlFunctions() ht.FuncMap

func MapTemplateFunc

func MapTemplateFunc(slice any, f any) (any, error)

func TextFunctions

func TextFunctions() tt.FuncMap

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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