Example #1
0
package array

import (
	"github.com/lestrrat/go-xslate/functions"
)

var depot = functions.NewFuncDepot("array")

func init() {
	depot.Set("Item", Item)
	depot.Set("Size", Size)
}

// Item returns the `i`-th item in the list
func Item(l []interface{}, i int) interface{} {
	return l[i]
}

// Size returns the size of the list
func Size(l []interface{}) int {
	return len(l)
}

// First returns the first element
func First(l []interface{}) interface{} {
	return l[0]
}

// Last returns the last element
func Last(l []interface{}) interface{} {
	return l[len(l)-1]
Example #2
0
package time

import (
	"github.com/lestrrat/go-xslate/functions"
	"time"
)

var depot = functions.NewFuncDepot("time")

func init() {
	depot.Set("After", time.After)
	depot.Set("Sleep", time.Sleep)
	depot.Set("Since", time.Since)
	depot.Set("Now", time.Now)
	depot.Set("ParseDuration", time.ParseDuration)
	depot.Set("Since", time.Since)
}

// Depot returns the FuncDepot in the "time" namespace
func Depot() *functions.FuncDepot {
	return depot
}
Example #3
0
package hash

import (
	"github.com/lestrrat/go-xslate/functions"
)

var depot = functions.NewFuncDepot("hash")

func init() {
	depot.Set("Keys", Keys)
}

// Keys returns the list of keys in this map. You can use this from a template
// like so `[% FOREACH key IN hash.Keys(mymap) %]...[% END %]` or
func Keys(m map[interface{}]interface{}) []interface{} {
	l := make([]interface{}, len(m))
	i := 0
	for k := range m {
		l[i] = k
		i++
	}
	return l
}

// Depot returns the Depot for hash package
func Depot() *functions.FuncDepot {
	return depot
}