Documentation ¶
Overview ¶
Package template implements helpers on-top of the stdlib `text/template`.
Index ¶
- func NewVarsFromPath(path string) (map[string]interface{}, error)
- type Template
- func (t *Template) Body() string
- func (t *Template) MustProcessString() string
- func (t *Template) Name() string
- func (t *Template) Process(dst io.Writer) error
- func (t *Template) ProcessString() (string, error)
- func (t *Template) SetVar(key string, value interface{})
- func (t *Template) SetVarsFromFile(path string) error
- func (t *Template) ViewFuncs() texttemplate.FuncMap
- func (t *Template) WithBody(body string) *Template
- func (t *Template) WithDelims(left, right string) *Template
- func (t *Template) WithEnvVars(envVars env.Vars) *Template
- func (t *Template) WithInclude(body string) *Template
- func (t *Template) WithName(name string) *Template
- func (t *Template) WithVar(key string, value interface{}) *Template
- func (t *Template) WithVars(vars Vars) *Template
- type Vars
- type ViewFuncs
- func (vf ViewFuncs) Add(values ...interface{}) (float64, error)
- func (vf ViewFuncs) AtIndex(index int, collection interface{}) (interface{}, error)
- func (vf ViewFuncs) Base64(v string) string
- func (vf ViewFuncs) Base64Decode(v string) (string, error)
- func (vf ViewFuncs) CSV(collection interface{}) (string, error)
- func (vf ViewFuncs) Ceil(d float64) float64
- func (vf ViewFuncs) Concat(strs ...string) string
- func (vf ViewFuncs) Contains(substr, v string) bool
- func (vf ViewFuncs) DateLong(t time.Time) string
- func (vf ViewFuncs) DateMonthDay(t time.Time) string
- func (vf ViewFuncs) DateShort(t time.Time) string
- func (vf ViewFuncs) DateShortRev(t time.Time) string
- func (vf ViewFuncs) Day(t time.Time) int
- func (vf ViewFuncs) Divide(values ...interface{}) (float64, error)
- func (vf ViewFuncs) DurationRound(d time.Duration, to time.Duration) time.Duration
- func (vf ViewFuncs) DurationRoundMillis(d time.Duration) time.Duration
- func (vf ViewFuncs) DurationRoundSeconds(d time.Duration) time.Duration
- func (vf ViewFuncs) FileExists(path string) bool
- func (vf ViewFuncs) First(collection interface{}) (interface{}, error)
- func (vf ViewFuncs) Floor(d float64) float64
- func (vf ViewFuncs) FormatFileSize(sizeBytes int) string
- func (vf ViewFuncs) FormatMoney(d float64) string
- func (vf ViewFuncs) FormatPct(d float64) string
- func (vf ViewFuncs) FuncMap() map[string]interface{}
- func (vf ViewFuncs) GenerateKey(keySize int) string
- func (vf ViewFuncs) GenerateOrdinalNames(format string, replicas int) []string
- func (vf ViewFuncs) HMAC512(key, v string) (string, error)
- func (vf ViewFuncs) HasPrefix(prefix, v string) bool
- func (vf ViewFuncs) HasSuffix(suffix, v string) bool
- func (vf ViewFuncs) Hour(t time.Time) int
- func (vf ViewFuncs) IndentSpaces(spaceCount int, v interface{}) string
- func (vf ViewFuncs) IndentTabs(tabCount int, v interface{}) string
- func (vf ViewFuncs) JSONEncode(v interface{}) (string, error)
- func (vf ViewFuncs) JSONEncodePretty(v interface{}) (string, error)
- func (vf ViewFuncs) Join(sep string, collection interface{}) (string, error)
- func (vf ViewFuncs) Last(collection interface{}) (interface{}, error)
- func (vf ViewFuncs) Matches(expr, v string) (bool, error)
- func (vf ViewFuncs) Millisecond(t time.Time) int
- func (vf ViewFuncs) Minute(t time.Time) int
- func (vf ViewFuncs) Month(t time.Time) int
- func (vf ViewFuncs) Multiply(values ...interface{}) (float64, error)
- func (vf ViewFuncs) Now() time.Time
- func (vf ViewFuncs) NowUTC() time.Time
- func (vf ViewFuncs) ParseBool(raw interface{}) (bool, error)
- func (vf ViewFuncs) ParseFloat64(v string) (float64, error)
- func (vf ViewFuncs) ParseInt(v interface{}) (int, error)
- func (vf ViewFuncs) ParseInt64(v interface{}) (int64, error)
- func (vf ViewFuncs) ParseJSON(v string) (interface{}, error)
- func (vf ViewFuncs) ParseSemver(v string) (*semver.Version, error)
- func (vf ViewFuncs) ParseTime(format, v string) (time.Time, error)
- func (vf ViewFuncs) ParseURL(v string) (*url.URL, error)
- func (vf ViewFuncs) ParseUUID(v string) (uuid.UUID, error)
- func (vf ViewFuncs) ParseUnix(v int64) time.Time
- func (vf ViewFuncs) ParseYAML(v string) (interface{}, error)
- func (vf ViewFuncs) Prefix(pref, v string) string
- func (vf ViewFuncs) Process(vm Viewmodel, contents string) (string, error)
- func (vf ViewFuncs) Quote(v string) string
- func (vf ViewFuncs) RFC3339(t time.Time) string
- func (vf ViewFuncs) RandomLetters(length int) string
- func (vf ViewFuncs) RandomLettersWithNumbers(count int) string
- func (vf ViewFuncs) RandomLettersWithNumbersAndSymbols(count int) string
- func (vf ViewFuncs) ReadFile(path string) (string, error)
- func (vf ViewFuncs) Reverse(collection interface{}) (interface{}, error)
- func (vf ViewFuncs) Round(places, d float64) float64
- func (vf ViewFuncs) SHA256(v string) string
- func (vf ViewFuncs) SHA512(v string) string
- func (vf ViewFuncs) Second(t time.Time) int
- func (vf ViewFuncs) SemverBumpMajor(v *semver.Version) *semver.Version
- func (vf ViewFuncs) SemverBumpMinor(v *semver.Version) *semver.Version
- func (vf ViewFuncs) SemverBumpPatch(v *semver.Version) *semver.Version
- func (vf ViewFuncs) SemverMajor(v *semver.Version) int
- func (vf ViewFuncs) SemverMinor(v *semver.Version) int
- func (vf ViewFuncs) SemverPatch(v *semver.Version) int
- func (vf ViewFuncs) SequenceInts(start, end int) []int
- func (vf ViewFuncs) Since(t time.Time) time.Duration
- func (vf ViewFuncs) SinceUTC(t time.Time) time.Duration
- func (vf ViewFuncs) Slice(from, to int, collection interface{}) (interface{}, error)
- func (vf ViewFuncs) Slugify(v string) string
- func (vf ViewFuncs) Split(sep, v string) []string
- func (vf ViewFuncs) SplitN(sep string, n float64, v string) []string
- func (vf ViewFuncs) StripQuotes(v string) string
- func (vf ViewFuncs) Subtract(values ...interface{}) (float64, error)
- func (vf ViewFuncs) Suffix(suf, v string) string
- func (vf ViewFuncs) TSV(collection interface{}) (string, error)
- func (vf ViewFuncs) TimeFormat(format string, t time.Time) string
- func (vf ViewFuncs) TimeInLocation(loc string, t time.Time) (time.Time, error)
- func (vf ViewFuncs) TimeInUTC(t time.Time) time.Time
- func (vf ViewFuncs) TimeIsEpoch(t time.Time) bool
- func (vf ViewFuncs) TimeIsZero(t time.Time) bool
- func (vf ViewFuncs) TimeKitchen(t time.Time) string
- func (vf ViewFuncs) TimeMedium(t time.Time) string
- func (vf ViewFuncs) TimeShort(t time.Time) string
- func (vf ViewFuncs) TimeSub(t1, t2 time.Time) time.Duration
- func (vf ViewFuncs) ToBytes(v interface{}) []byte
- func (vf ViewFuncs) ToDuration(val interface{}) (typedVal time.Duration, err error)
- func (vf ViewFuncs) ToFloat64(val interface{}) (typedVal float64, err error)
- func (vf ViewFuncs) ToInt(val interface{}) (typedVal int, err error)
- func (vf ViewFuncs) ToLower(v string) string
- func (vf ViewFuncs) ToString(v interface{}) string
- func (vf ViewFuncs) ToTitle(v string) string
- func (vf ViewFuncs) ToUpper(v string) string
- func (vf ViewFuncs) TrimPrefix(prefix, v string) string
- func (vf ViewFuncs) TrimSpace(v string) string
- func (vf ViewFuncs) TrimSuffix(suffix, v string) string
- func (vf ViewFuncs) URLEncode(value string) string
- func (vf ViewFuncs) URLHost(v *url.URL) string
- func (vf ViewFuncs) URLPath(v *url.URL) string
- func (vf ViewFuncs) URLPort(v *url.URL) string
- func (vf ViewFuncs) URLQuery(name string, v *url.URL) string
- func (vf ViewFuncs) URLRawQuery(v *url.URL) string
- func (vf ViewFuncs) URLScheme(v *url.URL) string
- func (vf ViewFuncs) UUIDv4() uuid.UUID
- func (vf ViewFuncs) Unix(t time.Time) int64
- func (vf ViewFuncs) UnixNano(t time.Time) int64
- func (vf ViewFuncs) WithURLHost(host string, v *url.URL) *url.URL
- func (vf ViewFuncs) WithURLPath(path string, v *url.URL) *url.URL
- func (vf ViewFuncs) WithURLPort(port string, v *url.URL) *url.URL
- func (vf ViewFuncs) WithURLQuery(key, value string, v *url.URL) *url.URL
- func (vf ViewFuncs) WithURLRawQuery(rawQuery string, v *url.URL) *url.URL
- func (vf ViewFuncs) WithURLScheme(scheme string, v *url.URL) *url.URL
- func (vf ViewFuncs) YAMLEncode(v interface{}) (string, error)
- func (vf ViewFuncs) Year(t time.Time) int
- type Viewmodel
- func (vm Viewmodel) Env(key string, defaults ...string) (string, error)
- func (vm Viewmodel) ExpandEnv(s string) string
- func (vm Viewmodel) Has(key string) bool
- func (vm Viewmodel) HasEnv(key string) bool
- func (vm Viewmodel) HasVar(key string) bool
- func (vm Viewmodel) Var(key string, defaults ...interface{}) (interface{}, error)
- func (vm Viewmodel) Vars() Vars
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewVarsFromPath ¶ added in v0.3.0
NewVarsFromPath returns a new vars file from a given path.
Types ¶
type Template ¶
type Template struct { Viewmodel // contains filtered or unexported fields }
Template is a wrapper for html.Template.
func NewFromFile ¶
NewFromFile creates a new template from a file.
func (*Template) MustProcessString ¶ added in v0.3.2
MustProcessString is a helper to process a template as a string and panic on error.
func (*Template) Name ¶
Name returns the template name if set, or if not set, just "template" as a constant.
func (*Template) ProcessString ¶ added in v0.3.2
ProcessString is a helper to process the template as a string.
func (*Template) SetVarsFromFile ¶ added in v0.3.0
SetVarsFromFile reads vars from a file and merges them with the current variables set.
func (*Template) ViewFuncs ¶
func (t *Template) ViewFuncs() texttemplate.FuncMap
ViewFuncs returns the view funcs.
func (*Template) WithBody ¶
WithBody sets the template body and returns a reference to the template object.
func (*Template) WithDelims ¶
WithDelims sets the template action delimiters, treating empty string as default delimiter.
func (*Template) WithEnvVars ¶ added in v0.3.0
WithEnvVars sets the environment variables.
func (*Template) WithInclude ¶
WithInclude includes a (sub) template into the rendering assets.
type ViewFuncs ¶
type ViewFuncs struct{}
ViewFuncs is the type stub for view functions.
var (
Funcs ViewFuncs
)
DefaultViewFuncs is a singleton for viewfuncs.
func (ViewFuncs) Base64Decode ¶ added in v0.2.0
Base64Decode decodes a base 64 string.
func (ViewFuncs) Contains ¶ added in v0.2.0
Contains returns if a string contains a given substring.
func (ViewFuncs) DateMonthDay ¶ added in v0.2.0
DateMonthDay returns the month dat format for a timestamp. The format string is "1/2".
func (ViewFuncs) DateShort ¶ added in v0.2.0
DateShort returns the short date for a timestamp. The format string is "1/02/2006"
func (ViewFuncs) DateShortRev ¶ added in v1.20201204.1
DateShortRev returns the short date for a timestamp in YYYY/mm/dd format.
func (ViewFuncs) DurationRound ¶ added in v1.20201204.1
DurationRound rounds a duration value.
func (ViewFuncs) DurationRoundMillis ¶ added in v1.20201204.1
DurationRoundMillis rounds a duration value to milliseconds.
func (ViewFuncs) DurationRoundSeconds ¶ added in v1.20201204.1
DurationRoundSeconds rounds a duration value to seconds.
func (ViewFuncs) FileExists ¶ added in v0.2.0
FileExists returns if the file at a given path exists.
func (ViewFuncs) FormatFileSize ¶ added in v1.20210701.2
FormatFileSize formats an int as a file size.
func (ViewFuncs) FormatMoney ¶ added in v0.2.0
FormatMoney returns a float as a formatted string rounded to two decimal places.
func (ViewFuncs) FormatPct ¶ added in v0.2.0
FormatPct formats a float as a percentage (it is multiplied by 100, then suffixed with '%')
func (ViewFuncs) GenerateKey ¶ added in v0.2.0
GenerateKey generates a key of a given size base 64 encoded.
func (ViewFuncs) GenerateOrdinalNames ¶ added in v0.2.0
GenerateOrdinalNames generates ordinal names by passing the index to a given formatter. The formatter should be in Sprintf format (i.e. using a '%d' token for where the index should go).
Example:
{{ generate_ordinal_names "worker-%d" 3 }} // [worker-0 worker-1 worker-2]
func (ViewFuncs) IndentSpaces ¶ added in v0.2.0
IndentSpaces indents a string by a given set of spaces.
func (ViewFuncs) IndentTabs ¶ added in v0.2.0
IndentTabs indents a string with a given number of tabs.
func (ViewFuncs) JSONEncode ¶ added in v0.2.0
JSONEncode returns an object encoded as json.
func (ViewFuncs) JSONEncodePretty ¶ added in v0.2.0
JSONEncodePretty encodes an object as json with indentation.
func (ViewFuncs) Matches ¶ added in v0.2.0
Matches returns if a string matches a given regular expression.
func (ViewFuncs) Millisecond ¶ added in v0.2.0
Millisecond returns the millisecond component of a timestamp.
func (ViewFuncs) ParseBool ¶ added in v0.2.0
ParseBool attempts to parse a value as a bool. "truthy" values include "true", "1", "yes". "falsey" values include "false", "0", "no".
func (ViewFuncs) ParseFloat64 ¶ added in v1.20201204.1
ParseFloat64 parses a value as a float64.
func (ViewFuncs) ParseInt64 ¶ added in v1.20201204.1
ParseInt64 parses a value as an int64.
func (ViewFuncs) ParseSemver ¶ added in v0.2.0
ParseSemver parses a semantic version string.
func (ViewFuncs) Process ¶ added in v0.3.0
Process processes the given contents using a given template viewmodel
func (ViewFuncs) Quote ¶ added in v0.3.2
Quote returns a string wrapped in " characters. It will trim space before and after, and only add quotes if they don't already exist.
func (ViewFuncs) RandomLetters ¶ added in v0.2.0
RandomLetters returns a string of random letters.
func (ViewFuncs) RandomLettersWithNumbers ¶ added in v0.2.0
RandomLettersWithNumbers returns a string of random letters.
func (ViewFuncs) RandomLettersWithNumbersAndSymbols ¶ added in v0.2.0
RandomLettersWithNumbersAndSymbols returns a string of random letters.
func (ViewFuncs) Round ¶ added in v0.2.0
Round returns the value rounded to a given set of places. It uses midpoint rounding.
func (ViewFuncs) SemverBumpMajor ¶ added in v0.2.0
SemverBumpMajor returns a semver with an incremented major version.
func (ViewFuncs) SemverBumpMinor ¶ added in v0.2.0
SemverBumpMinor returns a semver with an incremented minor version.
func (ViewFuncs) SemverBumpPatch ¶ added in v0.2.0
SemverBumpPatch returns a semver with an incremented patch version.
func (ViewFuncs) SemverMajor ¶ added in v0.2.0
SemverMajor returns the major component of a semver.
func (ViewFuncs) SemverMinor ¶ added in v0.2.0
SemverMinor returns the minor component of a semver.
func (ViewFuncs) SemverPatch ¶ added in v0.2.0
SemverPatch returns the patch component of a semver.
func (ViewFuncs) SequenceInts ¶ added in v1.20201204.1
SequenceInts returns an array of ints from min to max, not including max. Given (0,5) as inputs, it would return [0,1,2,3,4]
func (ViewFuncs) Since ¶ added in v0.2.0
Since returns the duration since a given timestamp. It is relative, meaning the value returned can be negative.
func (ViewFuncs) SinceUTC ¶ added in v0.2.0
SinceUTC returns the duration since a given timestamp in UTC. It is relative, meaning the value returned can be negative.
func (ViewFuncs) Slugify ¶ added in v1.20201204.1
Slugify returns a slug format string. It replaces whitespace with `-` It path escapes any other characters.
func (ViewFuncs) SplitN ¶ added in v0.2.0
SplitN splits a string by a separator a given number of times.
func (ViewFuncs) StripQuotes ¶ added in v1.0.0
StripQuotes strips leading and trailing quotes.
func (ViewFuncs) TimeFormat ¶ added in v1.0.0
TimeFormat returns the time with a given format string.
func (ViewFuncs) TimeInLocation ¶ added in v0.2.0
TimeInLocation returns the time in a given location by string. If the location is invalid, this will error.
func (ViewFuncs) TimeInUTC ¶ added in v1.0.0
TimeInUTC returns the time in a given location by string. If the location is invalid, this will error.
func (ViewFuncs) TimeIsEpoch ¶ added in v1.20210701.2
TimeIsEpoch returns if the time is the unix epoch time or not.
func (ViewFuncs) TimeIsZero ¶ added in v1.20210701.2
TimeIsZero returns if the time is set or not.
func (ViewFuncs) TimeKitchen ¶ added in v0.2.0
TimeKitchen returns the kitchen format for a timestamp. The format string is "3:04PM".
func (ViewFuncs) TimeMedium ¶ added in v0.2.0
TimeMedium returns the medium format for a timestamp. The format string is "1/02/2006 3:04:05 PM".
func (ViewFuncs) TimeShort ¶ added in v0.2.0
TimeShort returns the short format for a timestamp. The format string is "1/02/2006 3:04:05 PM".
func (ViewFuncs) TimeSub ¶ added in v1.20210701.2
TimeSub the duration difference between two times.
func (ViewFuncs) ToBytes ¶ added in v0.2.0
ToBytes attempts to return a bytes representation of a value.
func (ViewFuncs) ToDuration ¶ added in v1.20210204.2
ToDuration returns a given value as a duration.
func (ViewFuncs) ToString ¶ added in v0.2.0
ToString attempts to return a string representation of a value.
func (ViewFuncs) TrimPrefix ¶ added in v1.0.0
TrimPrefix returns if a string has a given prefix.
func (ViewFuncs) TrimSpace ¶ added in v0.2.0
TrimSpace trims whitespace from the beginning and end of a string.
func (ViewFuncs) TrimSuffix ¶ added in v1.0.0
TrimSuffix returns if a string has a given suffix.
func (ViewFuncs) URLPort ¶ added in v0.2.0
URLPort returns the url port. If none is explicitly specified, this will return empty string.
func (ViewFuncs) URLRawQuery ¶ added in v0.2.0
URLRawQuery returns the url raw query.
func (ViewFuncs) UnixNano ¶ added in v1.20201204.1
UnixNano returns the timetamp as nanoseconds from 1970-01-01.
func (ViewFuncs) WithURLHost ¶ added in v0.2.0
WithURLHost returns the host of a url.
func (ViewFuncs) WithURLPath ¶ added in v0.2.0
WithURLPath returns the url path.
func (ViewFuncs) WithURLPort ¶ added in v0.2.0
WithURLPort sets the url port.
func (ViewFuncs) WithURLQuery ¶ added in v0.2.0
WithURLQuery returns a url query param.
func (ViewFuncs) WithURLRawQuery ¶ added in v0.2.0
WithURLRawQuery returns the url path.
func (ViewFuncs) WithURLScheme ¶ added in v0.2.0
WithURLScheme returns the scheme of a url.
func (ViewFuncs) YAMLEncode ¶ added in v0.2.0
YAMLEncode returns an object encoded as yaml.
type Viewmodel ¶ added in v0.3.0
type Viewmodel struct {
// contains filtered or unexported fields
}
Viewmodel is the template viewmodel. It surfaces a subset of the template api. It is set / accessed by the outer template.
func (Viewmodel) ExpandEnv ¶ added in v0.3.0
ExpandEnv replaces $var or ${var} based on the configured environment variables.