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 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 RegisterCondFn(name string, cond CondFn)
- func RegisterCondOKFn(name string, cond CondOKFn)
- func RegisterEmptyCheckFn(name string, cond EmptyCheckFn)
- func RegisterModFn(name, alias string, mod ModFn)
- 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 RegisterVarInsPair(varName string, ins inspector.Inspector)
- 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 WriteFallback(w io.Writer, key, fbKey string, ctx *Ctx) (err error)
- type CondFn
- type CondOKFn
- 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) I18n(locale string, db *i18n.DB)
- func (ctx *Ctx) Reset()
- func (ctx *Ctx) Set(key string, val any, ins inspector.Inspector)
- func (ctx *Ctx) SetBytes(key string, val []byte)
- func (ctx *Ctx) SetCounter(key string, val int)
- func (ctx *Ctx) SetStatic(key string, val any)
- func (ctx *Ctx) SetString(key, val string)
- type CtxPool
- type EmptyCheckFn
- type ModFn
- type Node
- type Op
- type Parser
- type Pool
- type RangeLoop
- type Tpl
- type Tree
- type Type
Constants ¶
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 TypeLocale Type = 24 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 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 RegisterCondFn ¶
RegisterCondFn registers new condition helper in registry.
func RegisterCondOKFn ¶
RegisterCondOKFn registers new condition-OK helper in registry.
func RegisterEmptyCheckFn ¶
func RegisterEmptyCheckFn(name string, cond EmptyCheckFn)
RegisterEmptyCheckFn registers new empty check helper.
func RegisterModFn ¶
RegisterModFn registers new modifier function.
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 RegisterVarInsPair ¶
RegisterVarInsPair registers new variable-inspector pair.
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.
Types ¶
type CondOKFn ¶
CondOKFn describes helper func signature.
func GetCondOKFn ¶
GetCondOKFn returns condition-OK helper from the registry.
type Ctx ¶
type Ctx struct { // External buffers to use in modifier and condition helpers. BufAcc bytebuf.AccumulativeBuf // todo remove as unused later Buf, Buf1, Buf2 bytebuf.ChainBuf BufB bool BufI int64 BufU uint64 BufF float64 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 EmptyCheckFn ¶
EmptyCheckFn describes empty check helper func signature.
func GetEmptyCheckFn ¶
func GetEmptyCheckFn(name string) *EmptyCheckFn
GetEmptyCheckFn gets empty check helper from the registry.
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 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).
Source Files ¶
- cond.go
- condOK.go
- cond_builtin.go
- conv.go
- ctx.go
- ctx_pool.go
- db.go
- dyntpl.go
- empty_check.go
- empty_check_builtin.go
- error.go
- generate.go
- i18n.go
- init.go
- ipool.go
- mod.go
- mod_attr.go
- mod_builtin.go
- mod_css.go
- mod_html.go
- mod_js1.go
- mod_json.go
- mod_uri.go
- parser.go
- rloop.go
- static.go
- tree.go
- tree_node.go
- tree_types.go
- var_ins.go