Documentation
¶
Overview ¶
Package tpl provides a template engine with string interpolation and control structures.
Package tpl provides a template engine with string interpolation and control structures.
Package tpl provides a template engine with string interpolation and control structures.
Package tpl provides a template engine with string interpolation and control structures.
Index ¶
- Variables
- func AsOutValue(ctx context.Context, v interface{}) *interfaceValue
- func CallFunction(ctx context.Context, funcName string, params Values, target WritableValue) error
- func CompareValues(ctx context.Context, o1, o2 interface{}) (bool, error)
- func FormatSize(v uint64) string
- func LogDebug(ctx context.Context, msg string, arg ...any)
- func LogError(ctx context.Context, err error, msg string, arg ...any)
- func LogWarn(ctx context.Context, msg string, arg ...any)
- func QueryEscapeAny(ctx context.Context, val interface{}) string
- func RegisterFilter(name string, f TplFiltCallback)
- func RegisterFunction(name string, f *TplFunction)
- func ResetServerCtx(ctx context.Context)
- func ResolveValueIndex(ctx context.Context, v any, s string) (any, error)
- func ServerCtx(request *http.Request) context.Context
- func ValuesCtx(parent context.Context, values map[string]interface{}) context.Context
- func ValuesCtxAlways(parent context.Context, values map[string]interface{}) context.Context
- type ArrayAccessGet
- type ArrayAccessGetAny
- type CtxLog
- type Error
- type Page
- func (e *Page) Compile(ctx context.Context) error
- func (e *Page) Dump(o io.Writer, lvl int)
- func (e *Page) GetMime() string
- func (e *Page) GetProperty(p string) string
- func (e *Page) HasTpl(tpl string) bool
- func (e *Page) Parse(ctx context.Context, tpl string, out *interfaceValue) error
- func (e *Page) ParseAndReturn(ctx context.Context, tpl string) (string, error)
- func (e *Page) ParseAndWrite(ctx context.Context, tpl string, out io.Writer) error
- type RawData
- type TplCtxValue
- type TplFiltCallback
- type TplFuncCallback
- type TplFunction
- type Value
- type ValueCtx
- func (v *ValueCtx) Bytes() []byte
- func (v *ValueCtx) BytesErr() ([]byte, error)
- func (v *ValueCtx) IsString() bool
- func (v *ValueCtx) MarshalJSON() ([]byte, error)
- func (v *ValueCtx) MatchValueType(t interface{}) (interface{}, error)
- func (v *ValueCtx) Raw() (any, error)
- func (v *ValueCtx) String() string
- func (v *ValueCtx) StringErr() (string, error)
- func (v *ValueCtx) ToBool() bool
- func (v *ValueCtx) ToFloat() (float64, bool)
- func (v *ValueCtx) ToInt() (int64, bool)
- type ValueReader
- type Values
- type WritableValue
Constants ¶
This section is empty.
Variables ¶
var BBCodeCompiler = bbcode.NewCompiler(true, true)
var ( // ErrTplNotFound is returned when a requested template is not found. ErrTplNotFound = errors.New("tpl: Template not found") )
Common template errors.
Functions ¶
func AsOutValue ¶
func CallFunction ¶
func FormatSize ¶
FormatSize converts a size in bytes to a human-readable format using IEC units.
func LogError ¶ added in v1.0.9
LogError logs an error with context information. It's designed to be used with template execution errors.
func LogWarn ¶
LogWarn logs a warning message using the logger from context if available, otherwise falls back to the default structured logger.
func QueryEscapeAny ¶
func RegisterFilter ¶
func RegisterFilter(name string, f TplFiltCallback)
func RegisterFunction ¶
func RegisterFunction(name string, f *TplFunction)
func ResetServerCtx ¶
Types ¶
type ArrayAccessGet ¶
ArrayAccessGet defines an interface for types that can retrieve a Value by string key.
type ArrayAccessGetAny ¶ added in v1.0.5
ArrayAccessGetAny defines an interface for types that can retrieve any value by string key.
type CtxLog ¶
type CtxLog interface {
// LogWarn logs a warning message with the given arguments
LogWarn(msg string, arg ...any)
}
CtxLog defines an interface for context-aware logging.
type Error ¶
type Error struct {
// Message describes the error
Message string
// Template is the name of the template where the error occurred
Template string
// Line is the line number where the error occurred
Line int
// Char is the character position where the error occurred
Char int
// Stack contains a stack trace, if available
Stack []byte
// Parent is the wrapped error, if any
Parent error
}
Error is a template error, containing details such as where an error occurred in the template source and details on the actual error. It implements the error interface and supports error wrapping with Unwrap().
func (*Error) Error ¶
Error returns a string representation of the error. This implements the error interface.
type Page ¶
type Page struct {
// Version will be populated on compile
Version int
// Raw contains the template source data
Raw RawData
// MaxProcess controls parallel execution of templates
// 0 means unlimited concurrency, 1 means serial execution
MaxProcess int
// contains filtered or unexported fields
}
Page represents a template engine instance containing compiled templates.
func New ¶
func New() *Page
New creates a new template engine instance. The returned Page is ready to have templates added to it and then compiled.
func (*Page) Compile ¶
Compile processes all raw templates and builds the internal representation. It returns an error if any template fails to compile.
func (*Page) GetMime ¶
GetMime returns the MIME type for the page. The default value is "text/html" if not explicitly set. If a charset is specified, it will be appended to the MIME type.
func (*Page) GetProperty ¶
GetProperty returns the value of a page property. Returns an empty string if the property doesn't exist.
func (*Page) Parse ¶
Parse executes the named template in the given context, writing output to the provided interfaceValue. Returns ErrTplNotFound if the template doesn't exist.
func (*Page) ParseAndReturn ¶
ParseAndReturn executes the named template in the given context and returns the result as a string. Returns an empty string and ErrTplNotFound if the template doesn't exist.
type RawData ¶
type RawData struct {
PageProperties map[string]string // typically Charset=UTF-8 Content_Type=text/html
TemplateData map[string]string // actual contents for templates, at least "main" should be there
}
RawData contains the raw (uncompiled) data for current template
type TplCtxValue ¶
type TplCtxValue int
TplCtxValue is used as a context key for template-related values.
const ( // TplCtxLog is the context key for logging interface TplCtxLog TplCtxValue = iota + 1 )
type TplFiltCallback ¶
type TplFuncCallback ¶
type TplFuncCallback func(ctx context.Context, params Values, out WritableValue) error
type TplFunction ¶
type TplFunction struct {
Method TplFuncCallback
CanCompile bool
}
type Value ¶
type Value interface {
ReadValue(ctx context.Context) (any, error)
WithCtx(ctx context.Context) *ValueCtx
}
Value defines an interface for template values that can be read and contextualized.
type ValueCtx ¶
type ValueCtx struct {
Value
// contains filtered or unexported fields
}
ValueCtx wraps a Value with its associated context.
func (*ValueCtx) IsString ¶
IsString checks if the raw value is either a string, or an interface made to return a string
func (*ValueCtx) MarshalJSON ¶
func (*ValueCtx) MatchValueType ¶
type ValueReader ¶ added in v1.0.2
ValueReader defines an interface for types that can read a value in a given context.