Documentation ¶
Index ¶
- Constants
- Variables
- func ConvBool(val any) (b bool, ok bool)
- func ConvBytes(val any) (b []byte, ok bool)
- func ConvBytesSlice(val any) (b [][]byte, ok bool)
- func ConvFloat(val any) (f float64, ok bool)
- func ConvInt(val any) (i int64, ok bool)
- func ConvStr(val any) (s string, ok bool)
- func ConvStrSlice(val any) (s []string, ok bool)
- func ConvUint(val any) (u uint64, ok bool)
- func Docgen(format DocgenFormat) ([]byte, error)
- func EmptyCheck(ctx *Ctx, val any) bool
- func EmptyCheckBool(_ *Ctx, val any) bool
- func EmptyCheckBytes(_ *Ctx, val any) bool
- func EmptyCheckBytesSlice(_ *Ctx, val any) bool
- func EmptyCheckFloat(_ *Ctx, val any) bool
- func EmptyCheckInt(_ *Ctx, val any) bool
- func EmptyCheckStr(_ *Ctx, val any) bool
- func EmptyCheckStrSlice(_ *Ctx, val any) bool
- func EmptyCheckUint(_ *Ctx, val any) bool
- func GetInsByVarName(varName string) (inspector.Inspector, bool)
- func GetInspector(varName, name string) (ins inspector.Inspector, err error)
- func RegisterPool(key string, pool Pool) error
- func RegisterTpl(id int, key string, tree *Tree)
- func RegisterTplID(id int, tree *Tree)
- func RegisterTplKey(key string, tree *Tree)
- func ReleaseCtx(ctx *Ctx)
- func Render(key string, ctx *Ctx) ([]byte, error)
- func RenderByID(id int, ctx *Ctx) ([]byte, error)
- func RenderFallback(key, fbKey string, ctx *Ctx) ([]byte, error)
- func Write(w io.Writer, key string, ctx *Ctx) (err error)
- func WriteByID(w io.Writer, id int, ctx *Ctx) (err error)
- func WriteDocgen(w io.Writer, format DocgenFormat) error
- func WriteFallback(w io.Writer, key, fbKey string, ctx *Ctx) (err error)
- type CondFn
- type CondFnTuple
- type CondOKFn
- type CondOKTuple
- type Ctx
- func (ctx *Ctx) AcquireFrom(pool string) (any, error)
- func (ctx *Ctx) BufModOut(buf *any, p []byte)
- func (ctx *Ctx) BufModStrOut(buf *any, s string)
- func (ctx *Ctx) Defer(fn func() error)
- func (ctx *Ctx) Get(path string) any
- func (ctx *Ctx) GetCounter(key string) int
- func (ctx *Ctx) Reset()
- func (ctx *Ctx) Set(key string, val any, ins inspector.Inspector) *Ctx
- func (ctx *Ctx) SetBytes(key string, val []byte) *Ctx
- func (ctx *Ctx) SetCounter(key string, val int) *Ctx
- func (ctx *Ctx) SetStatic(key string, val any) *Ctx
- func (ctx *Ctx) SetString(key, val string) *Ctx
- type CtxPool
- type DocgenFormat
- type EmptyCheckFn
- type EmptyCheckTuple
- type Global
- type GlobalTuple
- type KV
- type ModFn
- type ModFnTuple
- type Node
- type Op
- type Parser
- type Pool
- type RangeLoop
- type Tpl
- type Tree
- type Type
- type VarInsTuple
Constants ¶
const ( DocgenFormatMarkdown DocgenFormat = "markdown" DocgenFormatHTML = "html" DocgenFormatJSON = "json" )
const ( TypeRaw Type = 0 TypeTpl Type = 1 TypeCond Type = 2 TypeCondOK Type = 3 TypeCondTrue Type = 4 TypeCondFalse Type = 5 TypeLoopRange Type = 6 TypeLoopCount Type = 7 TypeBreak Type = 8 TypeLBreak Type = 9 TypeContinue Type = 10 TypeCtx Type = 11 TypeCounter Type = 12 TypeSwitch Type = 13 TypeCase Type = 14 TypeDefault Type = 15 TypeDiv Type = 16 TypeJsonQ Type = 17 TypeEndJsonQ Type = 18 TypeHtmlE Type = 19 TypeEndHtmlE Type = 20 TypeUrlEnc Type = 21 TypeEndUrlEnc Type = 22 TypeInclude Type = 23 TypeExit Type = 99 // Must be in sync with inspector.Op type. OpUnk Op = 0 OpEq Op = 1 OpNq Op = 2 OpGt Op = 3 OpGtq Op = 4 OpLt Op = 5 OpLtq Op = 6 OpInc Op = 7 OpDec Op = 8 )
Variables ¶
var ( ErrUnexpectedEOF = errors.New("unexpected end of file: control structure couldn't be closed") ErrUnknownCtl = errors.New("unknown ctl") ErrSenselessCond = errors.New("comparison of two static args") ErrCondHlpNotFound = errors.New("condition helper not found") ErrTplNotFound = errors.New("template not found") ErrInterrupt = errors.New("tpl processing interrupted") ErrModNoArgs = errors.New("empty arguments list") ErrModPoorArgs = errors.New("arguments list is too small") ErrModNoStr = errors.New("argument is not string or bytes") ErrWrongLoopLim = errors.New("wrong count loop limit argument") ErrWrongLoopCond = errors.New("wrong loop condition operation") ErrWrongLoopOp = errors.New("wrong loop operation") ErrBreakLoop = errors.New("break loop") ErrLBreakLoop = errors.New("lazybreak loop") ErrContLoop = errors.New("continue loop") ErrUnknownPool = errors.New("unknown pool") )
Functions ¶
func ConvBytesSlice ¶
ConvBytesSlice tries to convert value to slice of bytes.
func ConvStrSlice ¶
ConvStrSlice tries to convert value to string slice.
func Docgen ¶ added in v1.1.9
func Docgen(format DocgenFormat) ([]byte, error)
func EmptyCheck ¶
EmptyCheck tries to apply all known helpers over the val.
First acceptable helper will break next attempts.
func EmptyCheckBool ¶
EmptyCheckBool checks is val is an empty bool.
func EmptyCheckBytes ¶
EmptyCheckBytes checks is val is an empty bytes array.
func EmptyCheckBytesSlice ¶
EmptyCheckBytesSlice checks is val is an empty slice of bytes.
func EmptyCheckFloat ¶
EmptyCheckFloat checks is val is an empty float number.
func EmptyCheckInt ¶
EmptyCheckInt checks is val is an empty integer.
func EmptyCheckStr ¶
EmptyCheckStr checks is val is an empty string.
func EmptyCheckStrSlice ¶
EmptyCheckStrSlice checks is val is an empty slice of strings.
func EmptyCheckUint ¶
EmptyCheckUint checks is val is an empty unsigned integer.
func GetInsByVarName ¶
GetInsByVarName gets inspector by variable name.
func GetInspector ¶
GetInspector gets inspector by both variable name or inspector name.
func RegisterPool ¶ added in v1.1.6
RegisterPool adds new internal pool to the registry by given key.
func RegisterTpl ¶
RegisterTpl saves template by ID and key in the registry.
You may use to access to the template both ID or key. This function can be used in any time to register new templates or overwrite existing to provide dynamics.
func Render ¶
Render template with given key according given context.
See Write(). Recommend to use Write() together with byte buffer pool to avoid redundant allocations.
func RenderByID ¶
RenderByID renders template with given ID according context.
See WriteByID(). Recommend to use WriteByID() together with byte buffer pool to avoid redundant allocations.
func RenderFallback ¶
RenderFallback renders template using one of keys: key or fallback key.
See WriteFallback(). Using this func you can handle cases when some objects have custom templates and all other should use default templates. Example: template registry: * tplUser * tplUser-15 user object with id 15 Call of dyntpl.RenderFallback("tplUser-15", "tplUser", ctx) will take template tplUser-15 from registry. In other case, for user #4: call of dyntpl.WriteFallback("tplUser-4", "tplUser", ctx) will take default template tplUser from registry. Recommend to user WriteFallback().
func Write ¶
Write template with given key to given writer object.
Using this function together with byte buffer pool reduces allocations.
func WriteByID ¶
WriteByID writes template with given ID to given writer object.
Using this function together with byte buffer pool reduces allocations.
func WriteDocgen ¶ added in v1.1.9
func WriteDocgen(w io.Writer, format DocgenFormat) error
Types ¶
type CondFnTuple ¶ added in v1.1.9
type CondFnTuple struct {
// contains filtered or unexported fields
}
func RegisterCondFn ¶
func RegisterCondFn(name string, cond CondFn) *CondFnTuple
RegisterCondFn registers new condition helper.
func RegisterCondFnNS ¶ added in v1.1.8
func RegisterCondFnNS(namespace, name string, cond CondFn) *CondFnTuple
RegisterCondFnNS registers new condition helper in given namespace.
func (*CondFnTuple) WithDescription ¶ added in v1.1.9
func (t *CondFnTuple) WithDescription(desc string) *docgen
func (*CondFnTuple) WithExample ¶ added in v1.1.9
func (t *CondFnTuple) WithExample(example string) *docgen
type CondOKFn ¶
CondOKFn describes helper func signature.
func GetCondOKFn ¶
GetCondOKFn returns condition-OK helper from the registry.
type CondOKTuple ¶ added in v1.1.9
type CondOKTuple struct {
// contains filtered or unexported fields
}
func RegisterCondOKFn ¶
func RegisterCondOKFn(name string, cond CondOKFn) *CondOKTuple
RegisterCondOKFn registers new condition-OK helper.
func RegisterCondOKFnNS ¶ added in v1.1.8
func RegisterCondOKFnNS(namespace, name string, cond CondOKFn) *CondOKTuple
RegisterCondOKFnNS registers new condition-OK helper in given namespace.
func (*CondOKTuple) WithDescription ¶ added in v1.1.9
func (t *CondOKTuple) WithDescription(desc string) *docgen
func (*CondOKTuple) WithExample ¶ added in v1.1.9
func (t *CondOKTuple) WithExample(example string) *docgen
type Ctx ¶
type Ctx struct { // External buffers to use in modifier and condition helpers. BufAcc bytebuf.Accumulative // todo remove as unused later // DEPRECATED: use BufAcc instead. Buf, Buf1, Buf2 bytebuf.Chain BufB bool BufI int64 BufU uint64 BufF float64 BufT time.Time BufX any Err error // contains filtered or unexported fields }
Ctx is a context object. Contains list of variables available to inspect. In addition, has buffers to help develop new helpers without allocations.
func (*Ctx) AcquireFrom ¶ added in v1.1.6
AcquireFrom receives new variable from given pool and register it to return batch after finish template processing.
func (*Ctx) BufModStrOut ¶
BufModStrOut buffers mod output string.
func (*Ctx) Defer ¶ added in v1.1.6
Defer registers new deferred function.
Function will call after finishing template. todo: find a way how to avoid closure allocation.
func (*Ctx) Get ¶
Get arbitrary value from the context by path.
See Ctx.get(). Path syntax: <ctxVrName>[.<Field>[.<NestedField0>[....<NestedFieldN>]]] Examples: * user.Bio.Birthday * staticVar
func (*Ctx) GetCounter ¶
GetCounter gets int counter value.
func (*Ctx) Set ¶
Set the variable to context. Inspector ins should be corresponded to variable val.
func (*Ctx) SetBytes ¶
SetBytes sets bytes as static variable.
See Ctx.Set(). This is a special case to improve speed.
func (*Ctx) SetCounter ¶
SetCounter sets int counter as static variable.
See Ctx.Set(). This is a special case to support counters.
type CtxPool ¶
type CtxPool struct {
// contains filtered or unexported fields
}
CtxPool is a context pool.
var CP CtxPool
CP is a default instance of context pool. You may use it directly as dyntpl.CP.Get()/Put() or using functions AcquireCtx()/ReleaseCtx().
type DocgenFormat ¶ added in v1.1.9
type DocgenFormat string
type EmptyCheckFn ¶
EmptyCheckFn describes empty check helper func signature.
func GetEmptyCheckFn ¶
func GetEmptyCheckFn(name string) EmptyCheckFn
GetEmptyCheckFn gets empty check helper from the registry.
type EmptyCheckTuple ¶ added in v1.1.9
type EmptyCheckTuple struct {
// contains filtered or unexported fields
}
func RegisterEmptyCheckFn ¶
func RegisterEmptyCheckFn(name string, cond EmptyCheckFn) *EmptyCheckTuple
RegisterEmptyCheckFn registers new empty check helper.
func RegisterEmptyCheckFnNS ¶ added in v1.1.8
func RegisterEmptyCheckFnNS(namespace, name string, cond EmptyCheckFn) *EmptyCheckTuple
RegisterEmptyCheckFnNS registers new empty check helper.
func (*EmptyCheckTuple) WithDescription ¶ added in v1.1.9
func (t *EmptyCheckTuple) WithDescription(desc string) *docgen
func (*EmptyCheckTuple) WithExample ¶ added in v1.1.9
func (t *EmptyCheckTuple) WithExample(example string) *docgen
func (*EmptyCheckTuple) WithNote ¶ added in v1.1.9
func (t *EmptyCheckTuple) WithNote(note string) *docgen
type GlobalTuple ¶ added in v1.1.9
type GlobalTuple struct {
// contains filtered or unexported fields
}
func RegisterGlobal ¶ added in v1.1.8
func RegisterGlobal(name, alias string, val Global) *GlobalTuple
RegisterGlobal registers new global variable.
Caution! Globals registered after template parsing will take no effect.
func RegisterGlobalNS ¶ added in v1.1.8
func RegisterGlobalNS(namespace, name, alias string, val Global) *GlobalTuple
RegisterGlobalNS registers new global variable in given namespace.
func (*GlobalTuple) WithDescription ¶ added in v1.1.9
func (t *GlobalTuple) WithDescription(desc string) *docgen
func (*GlobalTuple) WithExample ¶ added in v1.1.9
func (t *GlobalTuple) WithExample(example string) *docgen
type ModFn ¶
ModFn describes signature of the modifier functions.
Arguments description: * ctx provides access to additional variables and various buffers to reduce allocations. * buf is a storage for final result after finishing modifier work. * val is a left side variable that preceded to call of modifier func, example: {%= val|mod(...) %} * args is a list of all arguments listed on modifier call.
type ModFnTuple ¶ added in v1.1.9
type ModFnTuple struct {
// contains filtered or unexported fields
}
func RegisterModFn ¶
func RegisterModFn(name, alias string, mod ModFn) *ModFnTuple
RegisterModFn registers new modifier function.
func RegisterModFnNS ¶ added in v1.1.8
func RegisterModFnNS(namespace, name, alias string, mod ModFn) *ModFnTuple
RegisterModFnNS registers new mod function in given namespace.
func (*ModFnTuple) WithDescription ¶ added in v1.1.9
func (t *ModFnTuple) WithDescription(desc string) *docgen
func (*ModFnTuple) WithExample ¶ added in v1.1.9
func (t *ModFnTuple) WithExample(example string) *docgen
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a description of template part. Every piece of the template, beginning from static text and finishing of complex structures (switch, loop, ...) Represents by this type.
type Pool ¶ added in v1.1.6
type Pool interface { Get() any Put(any) // Reset cleanups data before putting to the pool. Reset(any) }
Pool represents internal pool. In addition to native sync.Pool requires Reset() method.
type RangeLoop ¶
type RangeLoop struct {
// contains filtered or unexported fields
}
RangeLoop is a object that injects to inspector to perform range loop execution.
func NewRangeLoop ¶
NewRangeLoop makes new RL.
func (*RangeLoop) RequireKey ¶
RequireKey checks if node requires a key to store in the context.
type Tpl ¶
Tpl is a main template object. Template contains only parsed template and evaluation logic. All temporary and intermediate data should be store in context object to make using of templates thread-safe.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree structure that represents parsed template as list of nodes with childrens.
func (*Tree) HumanReadable ¶
HumanReadable builds human-readable view of the tree (currently in XML format).
type VarInsTuple ¶ added in v1.1.9
type VarInsTuple struct {
// contains filtered or unexported fields
}
func RegisterVarInsPair ¶
func RegisterVarInsPair(varName string, ins inspector.Inspector) *VarInsTuple
RegisterVarInsPair registers new variable-inspector pair.
func (*VarInsTuple) WithDescription ¶ added in v1.1.9
func (t *VarInsTuple) WithDescription(desc string) *docgen
func (*VarInsTuple) WithExample ¶ added in v1.1.9
func (t *VarInsTuple) WithExample(example string) *docgen
Source Files ¶
- cond.go
- condOK.go
- cond_builtin.go
- conv.go
- ctx.go
- ctx_pool.go
- db.go
- docgen.go
- dyntpl.go
- empty_check.go
- empty_check_builtin.go
- error.go
- generate.go
- global.go
- init.go
- ipool.go
- mod.go
- mod_attr.go
- mod_builtin.go
- mod_css.go
- mod_datetime.go
- mod_html.go
- mod_js1.go
- mod_json.go
- mod_math.go
- mod_uri.go
- parser.go
- rloop.go
- static.go
- tree.go
- tree_node.go
- tree_types.go
- var_ins.go