Documentation ¶
Index ¶
- func IsDefined(val Value) bool
- func Self(r *Renderer) map[string]func() (string, error)
- type BaseValue
- func (*BaseValue) Bool() bool
- func (*BaseValue) Contains(other Value) bool
- func (*BaseValue) EqualValueTo(other Value) bool
- func (*BaseValue) Escaped() string
- func (*BaseValue) Float() float64
- func (*BaseValue) GetItem(key any) Value
- func (*BaseValue) Index(i int) Value
- func (*BaseValue) Integer() int
- func (*BaseValue) Interface() any
- func (*BaseValue) IsBool() bool
- func (*BaseValue) IsCallable() bool
- func (*BaseValue) IsDict() bool
- func (*BaseValue) IsFloat() bool
- func (*BaseValue) IsInteger() bool
- func (*BaseValue) IsIterable() bool
- func (*BaseValue) IsList() bool
- func (*BaseValue) IsNil() bool
- func (*BaseValue) IsNumber() bool
- func (v *BaseValue) IsSafe() bool
- func (*BaseValue) IsSliceable() bool
- func (*BaseValue) IsString() bool
- func (*BaseValue) Items() []*Pair
- func (*BaseValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())
- func (*BaseValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), ...)
- func (*BaseValue) Keys() ValuesList
- func (*BaseValue) Len() int
- func (*BaseValue) ReflectValue() reflect.Value
- func (*BaseValue) SetItem(key string, value any)
- func (*BaseValue) Slice(i, j int) Value
- func (*BaseValue) String() string
- func (*BaseValue) Values() ValuesList
- type ChainedStrictUndefinedValue
- type ChainedUndefinedValue
- type Context
- type Dict
- type EvalConfig
- type Evaluator
- func (e *Evaluator) Eval(node parse.Expression) Value
- func (e *Evaluator) ExecuteFilter(fc *parse.FilterCall, v Value) Value
- func (e *Evaluator) ExecuteFilterByName(name string, in Value, params *VarArgs) Value
- func (e *Evaluator) ExecuteTest(tc *parse.TestCall, v Value) Value
- func (e *Evaluator) ExecuteTestByName(name string, in Value, params *VarArgs) Value
- type FilterFunction
- type FilterSet
- type GenericValue
- func (v *GenericValue) Bool() bool
- func (v *GenericValue) Contains(other Value) bool
- func (v *GenericValue) EqualValueTo(other Value) bool
- func (v *GenericValue) Escaped() string
- func (v *GenericValue) Float() float64
- func (v *GenericValue) GetItem(key any) Value
- func (v *GenericValue) Index(i int) Value
- func (v *GenericValue) Integer() int
- func (v *GenericValue) Interface() any
- func (v *GenericValue) IsBool() bool
- func (v *GenericValue) IsCallable() bool
- func (v *GenericValue) IsDict() bool
- func (v *GenericValue) IsFloat() bool
- func (v *GenericValue) IsInteger() bool
- func (v *GenericValue) IsIterable() bool
- func (v *GenericValue) IsList() bool
- func (v *GenericValue) IsNil() bool
- func (v *GenericValue) IsNumber() bool
- func (v *GenericValue) IsSliceable() bool
- func (v *GenericValue) IsString() bool
- func (v *GenericValue) Items() []*Pair
- func (v *GenericValue) Iterate(fn func(idx, count int, key, value Value) (cont bool), empty func())
- func (v *GenericValue) IterateOrder(fn func(idx, count int, key, value Value) (cont bool), empty func(), ...)
- func (v *GenericValue) Keys() ValuesList
- func (v *GenericValue) Len() int
- func (v *GenericValue) ReflectValue() reflect.Value
- func (v *GenericValue) SetItem(key string, value interface{})
- func (v *GenericValue) Slice(i, j int) Value
- func (v *GenericValue) String() string
- func (v *GenericValue) Type() reflect.Type
- func (v *GenericValue) Values() ValuesList
- type KVPair
- type Kwarg
- type Macro
- type MacroSet
- type NilValue
- func (*NilValue) Bool() bool
- func (*NilValue) Contains(other Value) bool
- func (*NilValue) EqualValueTo(other Value) bool
- func (*NilValue) Escaped() string
- func (*NilValue) Float() float64
- func (*NilValue) GetItem(key any) Value
- func (*NilValue) Index(i int) Value
- func (*NilValue) Integer() int
- func (*NilValue) Interface() any
- func (*NilValue) IsBool() bool
- func (*NilValue) IsCallable() bool
- func (*NilValue) IsDict() bool
- func (*NilValue) IsFloat() bool
- func (*NilValue) IsInteger() bool
- func (*NilValue) IsIterable() bool
- func (*NilValue) IsList() bool
- func (*NilValue) IsNil() bool
- func (*NilValue) IsNumber() bool
- func (*NilValue) IsSafe() bool
- func (*NilValue) IsSliceable() bool
- func (*NilValue) IsString() bool
- func (*NilValue) Items() []*Pair
- func (*NilValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())
- func (*NilValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), ...)
- func (*NilValue) Keys() ValuesList
- func (*NilValue) Len() int
- func (*NilValue) ReflectValue() reflect.Value
- func (*NilValue) SetItem(key string, value interface{})
- func (*NilValue) Slice(i, j int) Value
- func (*NilValue) String() string
- type Pair
- type ReducedVarArgs
- type Renderer
- func (r *Renderer) EndTag(trim *parse.Trim)
- func (r *Renderer) Eval(node parse.Expression) Value
- func (r *Renderer) Evaluator() *Evaluator
- func (r *Renderer) Execute() (err error)
- func (r *Renderer) ExecuteWrapper(wrapper *parse.WrapperNode) (err error)
- func (r *Renderer) Flush(lstrip bool)
- func (r *Renderer) FlushAndTrim(trim, lstrip bool)
- func (r *Renderer) Inherit() *Renderer
- func (r *Renderer) LStrip()
- func (r *Renderer) RenderValue(value Value)
- func (r *Renderer) StartTag(trim *parse.Trim, lstrip bool)
- func (r *Renderer) String() string
- func (r *Renderer) Tag(trim *parse.Trim, lstrip bool)
- func (r *Renderer) WriteString(txt string) int
- type Statement
- type StatementSet
- func (ss StatementSet) Exists(name string) bool
- func (ss *StatementSet) MustRegister(name string, parser parse.StatementParser)
- func (ss *StatementSet) Register(name string, parser parse.StatementParser) error
- func (ss *StatementSet) Replace(name string, parser parse.StatementParser) error
- func (ss *StatementSet) Update(other StatementSet) StatementSet
- type StrictUndefinedValue
- func (u *StrictUndefinedValue) Contains(other Value) bool
- func (u *StrictUndefinedValue) EqualValueTo(other Value) bool
- func (u *StrictUndefinedValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())
- func (u *StrictUndefinedValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), ...)
- func (u *StrictUndefinedValue) Len() int
- func (u *StrictUndefinedValue) String() string
- type Template
- type TemplateLoadFn
- type TemplateLoader
- type TestFunction
- type TestSet
- type TrimState
- type Undefined
- func NewChainedStrictUndefinedValue(varName, format string, args ...any) Undefined
- func NewChainedUndefinedValue(varName, format string, args ...any) Undefined
- func NewStrictUndefinedValue(varName, format string, args ...any) Undefined
- func NewUndefinedValue(name, format string, args ...any) Undefined
- type UndefinedFunc
- type UndefinedValue
- func (u UndefinedValue) Bool() bool
- func (u *UndefinedValue) EqualValueTo(other Value) bool
- func (*UndefinedValue) Escaped() string
- func (u UndefinedValue) Float() float64
- func (u UndefinedValue) GetItem(key any) Value
- func (u UndefinedValue) Hint() string
- func (u UndefinedValue) Index(i int) Value
- func (u UndefinedValue) Integer() int
- func (u *UndefinedValue) Items() []*Pair
- func (*UndefinedValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())
- func (*UndefinedValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), ...)
- func (u *UndefinedValue) Keys() ValuesList
- func (*UndefinedValue) Len() int
- func (u UndefinedValue) Set(key string, value interface{})
- func (u UndefinedValue) Slice(i, j int) Value
- func (*UndefinedValue) String() string
- func (*UndefinedValue) Undefined()
- func (u *UndefinedValue) Values() ValuesList
- func (u UndefinedValue) VariableName() string
- type Value
- type ValueFactory
- type ValueFunc
- type ValuesList
- type VarArgs
- func (va *VarArgs) Expect(args int, kwargs []*Kwarg) *ReducedVarArgs
- func (va *VarArgs) ExpectArgs(args int) *ReducedVarArgs
- func (va *VarArgs) ExpectKwArgs(kwargs []*Kwarg) *ReducedVarArgs
- func (va *VarArgs) ExpectNothing() *ReducedVarArgs
- func (va *VarArgs) First() Value
- func (va *VarArgs) GetKwarg(key string) Value
- func (va *VarArgs) HasKwarg(key string) bool
- func (va *VarArgs) SetKwarg(key string, value Value)
- func (va *VarArgs) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseValue ¶
type BaseValue struct {
// contains filtered or unexported fields
}
BaseValue serves as a base for value containers.
func NewBaseValue ¶
func NewBaseValue(valueFactory *ValueFactory, isSafe bool) *BaseValue
NewBaseValue creates a new BaseValue container.
func (*BaseValue) EqualValueTo ¶
func (*BaseValue) IsCallable ¶
func (*BaseValue) IsIterable ¶
func (*BaseValue) IsSliceable ¶
func (*BaseValue) IterateOrder ¶
func (*BaseValue) Keys ¶
func (*BaseValue) Keys() ValuesList
func (*BaseValue) ReflectValue ¶
func (*BaseValue) Values ¶
func (*BaseValue) Values() ValuesList
type ChainedStrictUndefinedValue ¶
type ChainedStrictUndefinedValue struct {
StrictUndefinedValue
}
ChainedStrictUndefinedValue is like ChainedUndefinedValue but throws an error on any other type of access.
func (*ChainedStrictUndefinedValue) GetItem ¶
func (u *ChainedStrictUndefinedValue) GetItem(key any) Value
Get returns the value for the given key.
type ChainedUndefinedValue ¶
type ChainedUndefinedValue struct {
UndefinedValue
}
ChainedUndefinedValue represents an undefined value that renders to an empty string. Most other access methods will throw an error. It is different from UndefinedValue in that it allows for chaining for `Get` calls.
func (*ChainedUndefinedValue) GetItem ¶
func (u *ChainedUndefinedValue) GetItem(key any) Value
Get returns the value for the given key.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
func NewContext ¶
func NewContext(data map[string]any, userData any, valueFactory *ValueFactory) *Context
func NewEmptyContext ¶
func NewEmptyContext(valueFactory *ValueFactory) *Context
type Dict ¶
type Dict struct {
Pairs []*Pair
}
Dict represents a mapping of key-value [Pair]s.
func (*Dict) Keys ¶
func (d *Dict) Keys() ValuesList
Keys returns a [ValueList] of keys contained in the dict.
type EvalConfig ¶
type EvalConfig struct { *parse.Config Globals map[string]any Filters *FilterSet Statements *StatementSet Tests *TestSet TemplateLoadFn TemplateLoadFn // ExtensionConfig stores configuration for extensions. ExtensionConfig map[string]ext.Inheritable // CustomTypes allows to add custom value representations for types that are // not supported by default. For example, if you want to resolve value from // a custom ordered map type, you can add a custom value representation for // that and implement the [GetItem] method. CustomTypes map[reflect.Type]ValueFunc // Undefined is the type of undefined values that the resolver returns when // a value is not found. Undefined UndefinedFunc // NewlineSequence defines the sequence that starts a newline. Must be one // of '\r', '\n' or '\r\n'. The default is '\n' which is a useful default // for Linux and OS X systems as well as web applications. NewlineSequence string // TrimBlocks will remove the first newline after a block (block, not // variable tag!), if set to true. Defaults to false. TrimBlocks bool // LstripBlocks will strip leading spaces and tabs from the start of a line // to a block, if set to true. Defaults to false. LstripBlocks bool // KeepTrailingNewline will preserve the trailing newline when rendering // templates, if set to true. The default is false, which causes a single // newline, if present, to be stripped from the end of the template. KeepTrailingNewline bool // Autoescape will escape XML/HTML automatically, if set to true. Defaults // to false. Autoescape bool }
EvalConfig is the configuration for the execution of a template.
func NewEvalConfig ¶
func NewEvalConfig() *EvalConfig
NewEvalConfig creates a new evaluator configuration.
func (EvalConfig) Inherit ¶
func (cfg EvalConfig) Inherit() *EvalConfig
Inherit copies the configuration and returns a new configuration.
type Evaluator ¶
type Evaluator struct { *EvalConfig Ctx *Context ValueFactory *ValueFactory Current parse.Node }
func (*Evaluator) ExecuteFilter ¶
func (e *Evaluator) ExecuteFilter(fc *parse.FilterCall, v Value) Value
ExecuteFilter execute a filter node
func (*Evaluator) ExecuteFilterByName ¶
ExecuteFilterByName execute a filter given its name
type FilterFunction ¶
FilterFunction is the type filter functions must fulfil
type FilterSet ¶
type FilterSet map[string]FilterFunction
func (*FilterSet) Register ¶
func (fs *FilterSet) Register(name string, fn FilterFunction) error
Register registers a new filter. If there's already a filter with the same name, Register will panic. You usually want to call this function in the filter's init() function: http://golang.org/doc/effective_go.html#init
See http://www.florian-schlachter.de/post/gonja/ for more about writing filters and tags.
type GenericValue ¶
type GenericValue struct { BaseValue // Value holds the actual value in form of a reflection value. Value reflect.Value // IndirectValue holds the indirect (resolved) value . This is used to avoid // resolving the pointer values more than once. IndirectValue reflect.Value // contains filtered or unexported fields }
GenericValue is a container for values of various types.
func (*GenericValue) Bool ¶
func (v *GenericValue) Bool() bool
Bool returns the underlying value as bool. Non bool values will be evaluated to true in the following cases:
- int != 0
- uint != 0
- float != 0.0
- len(array/chan/map/slice/string) > 0
- bool == true
- underlying value is a struct
Otherwise returns always FALSE.
func (*GenericValue) Contains ¶
func (v *GenericValue) Contains(other Value) bool
Contains reports whether the underlying value (which must be of type struct, map, string, array or slice) contains of another Value (e. g. used to check whether a struct contains of a specific field or a map contains a specific key).
func (*GenericValue) EqualValueTo ¶
func (v *GenericValue) EqualValueTo(other Value) bool
EqualValueTo reports whether two values are containing the same value or object.
func (*GenericValue) Escaped ¶
func (v *GenericValue) Escaped() string
Escaped returns the HTML escaped version of String()
func (*GenericValue) Float ¶
func (v *GenericValue) Float() float64
Float returns the underlying value as a float (converts the underlying value, if necessary). If it's not possible to convert the underlying value, it will return 0.0.
func (*GenericValue) GetItem ¶
func (v *GenericValue) GetItem(key any) Value
Get returns the value for the given key. If 'value' has no such key, the undefined value is returned.
func (*GenericValue) Index ¶
func (v *GenericValue) Index(i int) Value
func (*GenericValue) Integer ¶
func (v *GenericValue) Integer() int
Integer returns the underlying value as an integer (converts the underlying value, if necessary). If it's not possible to convert the underlying value, it will return 0.
func (*GenericValue) Interface ¶
func (v *GenericValue) Interface() any
Interface returns the underlying value as an interface{}.
func (*GenericValue) IsBool ¶
func (v *GenericValue) IsBool() bool
IsBool reports whether the underlying value is a bool.
func (*GenericValue) IsCallable ¶
func (v *GenericValue) IsCallable() bool
IsCallable reports whether the underlying value is a callable function.
func (*GenericValue) IsDict ¶
func (v *GenericValue) IsDict() bool
IsDict reports whether the underlying value is a dictionary.
func (*GenericValue) IsFloat ¶
func (v *GenericValue) IsFloat() bool
IsFloat reports whether the underlying value is a float.
func (*GenericValue) IsInteger ¶
func (v *GenericValue) IsInteger() bool
IsInteger reports whether the underlying value is an integer.
func (*GenericValue) IsIterable ¶
func (v *GenericValue) IsIterable() bool
IsIterable reports whether the underlying value is an iterable type. Iterable types are strings, lists and dictionaries.
func (*GenericValue) IsList ¶
func (v *GenericValue) IsList() bool
IsList reports whether the underlying value is a list.
func (*GenericValue) IsNil ¶
func (v *GenericValue) IsNil() bool
IsNil reports whether the underlying value is NIL.
func (*GenericValue) IsNumber ¶
func (v *GenericValue) IsNumber() bool
IsNumber reports whether the underlying value is either an integer or a float.
func (*GenericValue) IsSliceable ¶
func (v *GenericValue) IsSliceable() bool
IsSliceable reports whether the underlying value is of type array, slice or string. You normally would use IsSliceable() before using the Slice() operation.
func (*GenericValue) IsString ¶
func (v *GenericValue) IsString() bool
IsString reports whether the underlying value is a string.
func (*GenericValue) Items ¶
func (v *GenericValue) Items() []*Pair
Items returns a list items contained in v.
func (*GenericValue) Iterate ¶
func (v *GenericValue) Iterate(fn func(idx, count int, key, value Value) (cont bool), empty func())
Iterate iterates over a map, array, slice or a string. It calls the function's first argument for every value with the following arguments:
idx current 0-index count total amount of items key *Value for the key or item value *Value (only for maps, the respective value for a specific key)
If the underlying value has no items or is not one of the types above, the empty function (function's second argument) will be called.
func (*GenericValue) IterateOrder ¶
func (v *GenericValue) IterateOrder( fn func(idx, count int, key, value Value) (cont bool), empty func(), reverse bool, sorted bool, caseSensitive bool, )
IterateOrder behaves like [Value.Iterate], but can iterate through an array/slice/string in reverse. Does not affect the iteration through a map because maps don't have any particular order. However, you can force an order using the `sorted` keyword (and even use `reversed sorted`).
func (*GenericValue) Keys ¶
func (v *GenericValue) Keys() ValuesList
Keys returns a list of keys contained in v.
func (*GenericValue) Len ¶
func (v *GenericValue) Len() int
Len returns the length for an array, chan, map, slice or string. Otherwise it will return 0.
func (*GenericValue) ReflectValue ¶
func (v *GenericValue) ReflectValue() reflect.Value
ReflectValue returns the underlying reflect value.
func (*GenericValue) SetItem ¶
func (v *GenericValue) SetItem(key string, value interface{})
XXX: need to work on that
func (*GenericValue) Slice ¶
func (v *GenericValue) Slice(i, j int) Value
Slice slices an array, slice or string. Otherwise it will return an empty []int.
func (*GenericValue) String ¶
func (v *GenericValue) String() string
String returns a string for the underlying value. If this value is not of type string, gonja tries to convert it. Currently the following types for underlying values are supported:
- string
- int/uint (any size)
- float (any precision)
- bool
- array/slice
- map
- String() will be called on the underlying value if provided
NIL values will lead to an empty string. Unsupported types are leading to their respective type name.
func (*GenericValue) Type ¶
func (v *GenericValue) Type() reflect.Type
Type returns the type of the value.
func (*GenericValue) Values ¶
func (v *GenericValue) Values() ValuesList
Values returns a list of values contained in v.
type MacroSet ¶
func (*MacroSet) Register ¶
Register registers a new filter. If there's already a filter with the same name, Register will panic. You usually want to call this function in the filter's init() function: http://golang.org/doc/effective_go.html#init
type NilValue ¶
type NilValue struct {
BaseValue
}
NilValue represents a value that is nil.
func (*NilValue) EqualValueTo ¶
func (*NilValue) IsCallable ¶
func (*NilValue) IsIterable ¶
func (*NilValue) IsSliceable ¶
func (*NilValue) IterateOrder ¶
func (*NilValue) Keys ¶
func (*NilValue) Keys() ValuesList
func (*NilValue) ReflectValue ¶
type ReducedVarArgs ¶
type ReducedVarArgs struct { *VarArgs // contains filtered or unexported fields }
ReducedVarArgs represents pythonic variadic args/kwargs but values are reduced (ie. kwargs given as args are accessible by name)
func (*ReducedVarArgs) Error ¶
func (rva *ReducedVarArgs) Error() string
func (*ReducedVarArgs) IsError ¶
func (rva *ReducedVarArgs) IsError() bool
IsError returns true if there was an error on Expect call
type Renderer ¶
type Renderer struct { *EvalConfig Ctx *Context ValueFactory *ValueFactory Template *Template Root *parse.TemplateNode Current parse.Node Out *strings.Builder Trim *TrimState }
Renderer is a node visitor in charge of rendering a template.
func NewRenderer ¶
func NewRenderer(ctx *Context, valueVactory *ValueFactory, out *strings.Builder, cfg *EvalConfig, tpl *Template) *Renderer
NewRenderer initialize a new renderer
func (*Renderer) Execute ¶
Execute performs the render process by visiting every node and turning them into text.
func (*Renderer) ExecuteWrapper ¶
func (r *Renderer) ExecuteWrapper(wrapper *parse.WrapperNode) (err error)
ExecuteWrapper wraps the parse.Wrapper execution logic
func (*Renderer) FlushAndTrim ¶
FlushAndTrim trims the contents of the buffer according to the trim policy and flushes it to the final output.
func (*Renderer) RenderValue ¶
RenderValue renders a single value.
func (*Renderer) WriteString ¶
WriteString applies the trim policy on the given string and writes it to the buffer.
type Statement ¶
type Statement interface { parse.Statement Execute(*Renderer, *parse.StatementBlockNode) }
type StatementSet ¶
type StatementSet map[string]parse.StatementParser
func (StatementSet) Exists ¶
func (ss StatementSet) Exists(name string) bool
Exists returns true if the given test is already registered
func (*StatementSet) MustRegister ¶
func (ss *StatementSet) MustRegister(name string, parser parse.StatementParser)
MustRegister is like Register but panics if the tag cannot be registered.
func (*StatementSet) Register ¶
func (ss *StatementSet) Register(name string, parser parse.StatementParser) error
Register registers a new tag. You usually want to call this function in the tag's init() function: http://golang.org/doc/effective_go.html#init
func (*StatementSet) Replace ¶
func (ss *StatementSet) Replace(name string, parser parse.StatementParser) error
Replace replaces an already registered tag with a new implementation. Use this function with caution since it allows you to change existing tag behavior.
func (*StatementSet) Update ¶
func (ss *StatementSet) Update(other StatementSet) StatementSet
Update updates the statement set with the given statement set.
type StrictUndefinedValue ¶
type StrictUndefinedValue struct {
UndefinedValue
}
StrictUndefinedValue represents an undefined value that throws an error when accessed.
func (*StrictUndefinedValue) Contains ¶
func (u *StrictUndefinedValue) Contains(other Value) bool
func (*StrictUndefinedValue) EqualValueTo ¶
func (u *StrictUndefinedValue) EqualValueTo(other Value) bool
func (*StrictUndefinedValue) Iterate ¶
func (u *StrictUndefinedValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())
func (*StrictUndefinedValue) IterateOrder ¶
func (u *StrictUndefinedValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), reverse, sorted, caseSensitive bool)
func (*StrictUndefinedValue) Len ¶
func (u *StrictUndefinedValue) Len() int
func (*StrictUndefinedValue) String ¶
func (u *StrictUndefinedValue) String() string
type Template ¶
type Template struct { Reader io.Reader Source string Env *EvalConfig // XXX: needed? Loader TemplateLoader Tokens *parse.Stream Parser *parse.Parser Root *parse.TemplateNode Macros MacroSet }
Template is the central template object. It represents a parsed template and is used to evaluate it.
func NewTemplate ¶
func NewTemplate(name, source string, cfg *EvalConfig) (*Template, error)
NewTemplate creates a new template.
func (*Template) Execute ¶
Execute executes the template with the given context and returns the rendered template as a string.
func (*Template) ExecuteBytes ¶
ExecuteBytes executes the template with the given context and returns the rendered template as []byte.
type TemplateLoadFn ¶
TemplateLoadFn is a function that loads a template by name.
type TemplateLoader ¶
TemplateLoader is an interface for loading templates by name.
type TestFunction ¶
TestFunction is the type test functions must fulfil
type TestSet ¶
type TestSet map[string]TestFunction
TestSet maps test names to their TestFunction handler
func (*TestSet) Register ¶
func (ts *TestSet) Register(name string, fn TestFunction) error
Register registers a new test. If there's already a test with the same name, RegisterTest will panic. You usually want to call this function in the test's init() function: http://golang.org/doc/effective_go.html#init
See http://www.florian-schlachter.de/post/gonja/ for more about writing tests and tags.
type Undefined ¶
type Undefined interface { Value // Undefind is a marker method to identify undefined values. Undefined() // VariableName returns the name of the variable that is undefined. VariableName() string // Hint returns a hint for the undefined variable. Hint() string }
Undefined is an interface that represents an Undefined value.
func NewChainedStrictUndefinedValue ¶
NewChainedStrictUndefinedValue creates a new ChainedStrictUndefinedValue.
func NewChainedUndefinedValue ¶
NewChainedUndefinedValue creates a new ChainedUndefinedValue.
func NewStrictUndefinedValue ¶
NewStrictUndefinedValue creates a new StrictUndefinedValue.
func NewUndefinedValue ¶
NewUndefinedValue creates a new UndefinedValue.
type UndefinedFunc ¶
UndefinedFunc is a function that creates a new Undefined value.
type UndefinedValue ¶
type UndefinedValue struct { BaseValue // contains filtered or unexported fields }
UndefinedValue represents an undefined value that renders to an empty string. Most other access methods will throw an error.
func (UndefinedValue) Bool ¶
func (u UndefinedValue) Bool() bool
func (*UndefinedValue) EqualValueTo ¶
func (u *UndefinedValue) EqualValueTo(other Value) bool
func (*UndefinedValue) Escaped ¶
func (*UndefinedValue) Escaped() string
func (UndefinedValue) Float ¶
func (u UndefinedValue) Float() float64
func (UndefinedValue) GetItem ¶
func (u UndefinedValue) GetItem(key any) Value
func (UndefinedValue) Hint ¶
func (u UndefinedValue) Hint() string
Hint returns a hint for the undefined variable.
func (UndefinedValue) Index ¶
func (u UndefinedValue) Index(i int) Value
func (UndefinedValue) Integer ¶
func (u UndefinedValue) Integer() int
func (*UndefinedValue) Items ¶
func (u *UndefinedValue) Items() []*Pair
func (*UndefinedValue) Iterate ¶
func (*UndefinedValue) Iterate(fn func(idx, count int, key, value Value) bool, empty func())
func (*UndefinedValue) IterateOrder ¶
func (*UndefinedValue) IterateOrder(fn func(idx, count int, key, value Value) bool, empty func(), reverse, sorted, caseSensitive bool)
func (*UndefinedValue) Keys ¶
func (u *UndefinedValue) Keys() ValuesList
func (*UndefinedValue) Len ¶
func (*UndefinedValue) Len() int
func (UndefinedValue) Set ¶
func (u UndefinedValue) Set(key string, value interface{})
func (UndefinedValue) Slice ¶
func (u UndefinedValue) Slice(i, j int) Value
func (*UndefinedValue) String ¶
func (*UndefinedValue) String() string
func (*UndefinedValue) Undefined ¶
func (*UndefinedValue) Undefined()
Undefined is a marker method to identify undefined values.
func (*UndefinedValue) Values ¶
func (u *UndefinedValue) Values() ValuesList
func (UndefinedValue) VariableName ¶
func (u UndefinedValue) VariableName() string
VariableName returns the name of the variable that is undefined.
type Value ¶
type Value interface { // IsString returns true if the value is a string, false otherwise. IsString() bool // IsBool returns true if the value is a boolean, false otherwise. IsBool() bool // IsFloat returns true if the value is a float, false otherwise. IsFloat() bool // IsInteger returns true if the value is an integer, false otherwise. IsInteger() bool // IsNumber returns true if the value is a number (integer or float), false // otherwise. IsNumber() bool // IsList returns true if the value is a list or array, false otherwise. IsList() bool // IsDict returns true if the value is a dictionary or map, false otherwise. IsDict() bool // IsNil returns true if the value is nil or null, false otherwise. IsNil() bool // IsSafe returns true if the value is safe for concurrent use, false // otherwise. IsSafe() bool // IsCallable returns true if the value is a callable function or method, // false otherwise. IsCallable() bool // IsIterable returns true if the value is iterable, false otherwise. IsIterable() bool // IsSliceable returns true if the value is sliceable, false otherwise. IsSliceable() bool // Interface returns the underlying value as an interface{}. Interface() any // ReflectValue returns the underlying reflect value. ReflectValue() reflect.Value // String returns the string representation of the value. String() string // Escaped returns the escaped string representation of the value. Escaped() string // Integer returns the integer representation of the value. Integer() int // Float returns the float representation of the value. Float() float64 // Bool returns the boolean representation of the value. Bool() bool // Len returns the length of the value, if it's a list, dictionary, or // string. Len() int // Slice returns a slice of the value, if it's a list or string, from index // i to j. Slice(i, j int) Value // Index returns the value at the given index, if it's a list or string. Index(i int) Value // Contains returns true if the value contains the given value. Contains(other Value) bool // Keys returns the keys of the underlying map. Keys() ValuesList // Values returns the values of the underlying map. Values() ValuesList // Items returns the key-value pairs of the underlying map. Items() []*Pair // GetItem returns the value associated with the given key. GetItem(key any) Value // SetItem sets the value associated with the given key to the given value. SetItem(key string, value any) // Iterate iterates over the value's items, if it's a list or dictionary, // and calls the provided function for each item. If the value is empty, the // empty function is called instead. Iterate(fn func(idx, count int, key, value Value) (cont bool), empty func()) // IterateOrder iterates over the value's items, if it's a dictionary, in a // specified order, and calls the provided function for each item. If the // value is empty, the empty function is called instead. If reverse is true, // the items are iterated in reverse order. If sorted is true, the items are // sorted by key. If caseSensitive is true, the keys are compared // case-sensitively. IterateOrder(fn func(idx, count int, key, value Value) (cont bool), empty func(), reverse, sorted, caseSensitive bool) // EqualValueTo returns true if the value is equal to the other value, false // otherwise. EqualValueTo(other Value) bool }
Value is the interface that all value containers must implement. You can use BaseValue as a base for your own value container implementations.
type ValueFactory ¶
type ValueFactory struct {
// contains filtered or unexported fields
}
ValueFactory is a factory for creating values.
func NewValueFactory ¶
func NewValueFactory(undefined UndefinedFunc, customTypes map[reflect.Type]ValueFunc) *ValueFactory
NewValueFactory creates a new value factory.
func (*ValueFactory) NewUndefined ¶
func (vf *ValueFactory) NewUndefined(name, hintFormat string, args ...any) Undefined
NewUndefined creates a new undefined value.
type ValueFunc ¶
type ValueFunc func(value any, safe bool, valueFactory *ValueFactory) Value
ValueFunc is a function that creates a new value container.
type ValuesList ¶
type ValuesList []Value
ValuesList represents a list of [Value]s.
func (ValuesList) Contains ¶
func (vl ValuesList) Contains(value Value) bool
Contains reports whether a value is within vl.
func (ValuesList) Len ¶
func (vl ValuesList) Len() int
Len is the number of elements in the collection.
func (ValuesList) Less ¶
func (vl ValuesList) Less(i, j int) bool
Less reports whether the element with index i must sort before the element with index j.
func (ValuesList) String ¶
func (vl ValuesList) String() string
String returns a string representation of vl in the form "['value1', 'value2']".
func (ValuesList) Swap ¶
func (vl ValuesList) Swap(i, j int)
Swap swaps the elements with indexes i and j.
type VarArgs ¶
type VarArgs struct { Args []Value Kwargs []KVPair ValueFactory *ValueFactory }
VarArgs represents pythonic variadic args/kwargs.
func NewVarArgs ¶
func NewVarArgs(valueFactory *ValueFactory) *VarArgs
NewVarArgs creates a new VarArgs.
func NewVarArgsWithValues ¶
func NewVarArgsWithValues(valueFactory *ValueFactory, args []Value, kwargs []KVPair) *VarArgs
NewVarArgsWithValues creates a new VarArgs from a list of Values.
func (*VarArgs) Expect ¶
func (va *VarArgs) Expect(args int, kwargs []*Kwarg) *ReducedVarArgs
Expect validates VarArgs against an expected signature
func (*VarArgs) ExpectArgs ¶
func (va *VarArgs) ExpectArgs(args int) *ReducedVarArgs
ExpectArgs ensures VarArgs receive only arguments
func (*VarArgs) ExpectKwArgs ¶
func (va *VarArgs) ExpectKwArgs(kwargs []*Kwarg) *ReducedVarArgs
ExpectKwArgs allow to specify optionally expected KwArgs
func (*VarArgs) ExpectNothing ¶
func (va *VarArgs) ExpectNothing() *ReducedVarArgs
ExpectNothing ensures VarArgs does not receive any argument
func (*VarArgs) GetKwarg ¶
GetKwarg gets a keyword argument. It panics if the keyword argument does not exist. Make sure to define all the expected keyword arguments before calling this.