Documentation
¶
Index ¶
- func ClearCache()
- func Pairs(args ...any) map[string]any
- func RegisterFunc(name string, fn any)
- func Render(src string, params ...any) string
- func RenderFile(path string, params ...any) string
- func RenderText(src string, params ...any) string
- func RenderWriter(w io.Writer, src string, params ...any)
- func SetCacheSize(n int)
- type ArrayLitExpr
- type AssignStmt
- type BinExpr
- type BreakStmt
- type Builder
- type CallExpr
- type CaseClause
- type CompoundAssignStmt
- type Context
- type ContinueStmt
- type EchoStmt
- type Engine
- type Expr
- type ExprStmt
- type ForCNode
- type ForRangeNode
- type IfNode
- type IncDecExpr
- type IncDecStmt
- type IncludeNode
- type IsSetExpr
- type LitExpr
- type MapLitExpr
- type Node
- type Stmt
- type StmtsNode
- type SwitchNode
- type TKind
- type Template
- type TernaryExpr
- type TextNode
- type Token
- type UnExpr
- type VarExpr
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Pairs ¶
Pairs converts a flat alternating key/value list into a map[string]any. Example: Pairs("name", "Alice", "age", 30) → map[string]any{"name":"Alice","age":30}
func RegisterFunc ¶
RegisterFunc registers a named function for use in templates. fn must be a function value of any signature. Arguments are coerced to the declared parameter types automatically. The return value is stringified. Functions may return one or two values; when two values are returned the last must implement error — a non-nil error suppresses any output.
A registered function can be used in three ways:
$fn(arg1, arg2) — explicit call; args may be $variables or literals ("str", 42, 3.14)
$var|fn — modifier: receives the variable's resolved value as first argument
$fn — standalone: called with no arguments when name is not in params
func RenderFile ¶
RenderFile loads path and executes it with the given params.
func RenderText ¶
RenderText compiles src and executes it with the given params. This is the engine-aware version of the existing Render() function.
func RenderWriter ¶
RenderWriter compiles src and writes the rendered output to w.
func SetCacheSize ¶
func SetCacheSize(n int)
SetCacheSize sets the maximum number of compiled templates to cache. Pass 0 to disable caching. If n is smaller than the current cache size, the cache is cleared.
Types ¶
type ArrayLitExpr ¶
type ArrayLitExpr struct{ Elems []Expr }
ArrayLitExpr is a literal array constructed in a code block: [expr, expr, ...]
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is the fluent API for loading and rendering templates.
tpl.File("path/to/file.html").Set(ctx).Render()
tpl.Text("Hello $name!").Set(ctx).Render()
func File ¶
File loads a template from a file path. The compiled Engine is cached by path, and the cache entry is invalidated automatically when the file's modification time changes. Returns a Builder whose context is empty until Set() is called.
func Text ¶
Text compiles a template from an inline string. The compiled Engine is cached by the full source string.
func (*Builder) RenderWriter ¶
RenderWriter executes the template and writes the output to w.
type CaseClause ¶
CaseClause is one arm of a switch statement. A single case may match multiple values: case "a", "b":
type CompoundAssignStmt ¶
CompoundAssignStmt is $var += expr / $var -= expr / $var *= expr / $var /= expr
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context holds the execution state during template rendering. Scopes form a chain: each for/if block gets a child context.
func (*Context) Get ¶
Get looks up a bare variable name (no dots or brackets). Checks scope chain first, then the original params.
func (*Context) GetIndex ¶
GetIndex resolves a path that may include a dynamic variable index at the end. E.g. GetIndex("m", VarExpr{Path:"k"}) → m[value-of-k]
type ContinueStmt ¶
type ContinueStmt struct{}
ContinueStmt skips to the next iteration of the nearest enclosing for loop.
type EchoStmt ¶
type EchoStmt struct{ X Expr }
EchoStmt outputs the value of X. echo $expr / print $expr
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is a compiled template ready for repeated execution.
func CompileEngine ¶
CompileEngine compiles src into an Engine.
type ExprStmt ¶
type ExprStmt struct{ X Expr }
ExprStmt evaluates X and outputs a non-nil result. Used for bare <? $name ?> and <? fn(args) ?>
type ForCNode ¶
type ForCNode struct {
Init Stmt // may be nil
Cond Expr // nil → infinite loop
Post Stmt // may be nil
Body []Node
}
ForCNode is <? for($i=0; $i<10; $i++){ ?> body <? } ?> Also serves as a while loop when Init and Post are nil: for($cond){
type ForRangeNode ¶
type ForRangeNode struct {
Key string // "" when only one variable
Val string
Iter Expr
Body []Node
}
ForRangeNode is <? for($key, $val := range $iter){ ?> body <? } ?>
type IncDecExpr ¶
IncDecExpr is $var++ or $var-- used inside a larger expression.
type IncDecStmt ¶
IncDecStmt is $var++ or $var--
type IncludeNode ¶
type IncludeNode struct {
Path Expr
}
IncludeNode is <? require("path") ?> or <? include("path") ?>
type IsSetExpr ¶
type IsSetExpr struct {
Path string
}
IsSetExpr reports whether a variable path is defined in the context. Produced by isset($path) in code blocks.
type LitExpr ¶
type LitExpr struct{ Val any }
LitExpr is a literal value: string, int64, float64, bool, or nil.
type MapLitExpr ¶
MapLitExpr is a literal map constructed in a code block: {"key": val, "key2": val2}
type StmtsNode ¶
type StmtsNode struct {
// contains filtered or unexported fields
}
StmtsNode is a list of statements from one <? ... ?> code block.
type SwitchNode ¶
type SwitchNode struct {
Val Expr
Cases []CaseClause
Default []Node
}
SwitchNode is <? switch($val){ ?> cases <? } ?>
type TKind ¶
type TKind uint8
TKind is a code-block token kind.
const ( TkEOF TKind = iota TkIdent // bare identifier: echo, for, range, translate, … TkVar // $name (val holds the name without '$') TkString // "…" or '…' (lit holds the parsed string value) TkInt // integer literal (lit holds int64) TkFloat // float literal (lit holds float64) TkOp // any operator or punctuation stored as a string TkLParen // ( TkRParen // ) TkLBrace // { TkRBrace // } TkLBracket // [ TkRBracket // ] TkComma // , TkSemicolon // ; (or newline acting as statement separator) )
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is a pre-compiled template ready for repeated execution.
func Parse ¶
Parse compiles src into a Template and caches the result up to the configured cache size.
type TernaryExpr ¶
TernaryExpr is cond ? then : else
type TextNode ¶
type TextNode struct {
// contains filtered or unexported fields
}
TextNode is literal template text with $var placeholders pre-scanned.