Documentation
¶
Index ¶
- Constants
- Variables
- func EvaluateExpression(expression string, context map[string]interface{}) (interface{}, error)
- func IsTruthy(value interface{}) bool
- func ParseAndEvaluate(expr string, context map[string]interface{}) (interface{}, error)
- func ParseVariables(template string) ([]string, error)
- func ParseVariablesFromExpression(expression string) ([]string, error)
- func TemplateString(template string, context map[string]interface{}) (string, error)
- func TemplateStringInContext(template string, context map[string]interface{}, searchDirs []string) (string, error)
- func TryEvaluateSingleExpressionTemplate(template string, context map[string]interface{}) (interface{}, bool, bool, error)
- type CompareOp
- type ControlTagInfo
- type ControlTagType
- type EvaluateExpressionFunc
- type Evaluator
- type ExprNode
- type ExprNodeType
- type ExprParser
- type FileResolver
- type FilterFunc
- type FunctionFunc
- type Lexer
- type Node
- type NodeType
- type OmitType
- type Parser
- type ProcessNodesFunc
- type TemplateCache
- type TestFunc
- type Token
- type TokenType
- type UndefinedType
Constants ¶
const ( PrecedenceOr = 10 PrecedenceAnd = 20 PrecedenceNot = 30 PrecedenceCompare = 40 PrecedenceAdd = 50 PrecedenceMul = 60 PrecedencePow = 70 )
Operator precedence levels (higher means higher precedence)
Variables ¶
var GlobalFilters map[string]FilterFunc
GlobalFilters stores the registered filter functions.
var GlobalFunctions map[string]FunctionFunc
GlobalFunctions stores the registered functions that can be called directly in templates
var GlobalMethods map[string]map[string]FunctionFunc
GlobalMethods stores methods that can be called on objects of specific types
var GlobalTests map[string]TestFunc
GlobalTests stores the registered Jinja test functions.
var Omit = OmitType{}
Omit is the singleton value representing the 'omit' keyword
var Undefined = UndefinedType{}
Undefined is the singleton value representing an undefined variable
Functions ¶
func EvaluateExpression ¶
EvaluateExpression evaluates a single expression string (without surrounding {{ }}) against the provided context. It applies filters as specified. If the variable is undefined after evaluation (and not handled by a filter like default), an error is returned.
func IsTruthy ¶ added in v0.1.2
func IsTruthy(value interface{}) bool
IsTruthy determines if a value is considered "truthy" in the Python/Jinja2 sense
func ParseAndEvaluate ¶
ParseAndEvaluate parses and evaluates an expression string with context
func ParseVariables ¶ added in v0.1.2
ParseVariables extracts all Jinja variable names from a template string. It returns a slice of unique variable names found in expressions {{ ... }} and control tags {% ... %}. For example, "some string with a {{ item.name | default('name') }}" returns ["item"].
func ParseVariablesFromExpression ¶ added in v0.1.7
ParseVariablesFromExpression extracts all root Jinja variable names from a Jinja expression string. For example, for the expression `item.some_key`, it returns ["item"]. For `item.some_bool and another_item`, it returns ["item", "another_item"].
func TemplateString ¶
TemplateString renders a template string using the provided context. It processes Jinja-like expressions {{ ... }}, comments {# ... #}, and control tags {% ... %}.
func TemplateStringInContext ¶ added in v0.3.0
func TemplateStringInContext(template string, context map[string]interface{}, searchDirs []string) (string, error)
TemplateStringInContext renders a template with custom directories used for file resolution. When searchDirs is non-empty, relative paths are resolved by checking each directory in order.
func TryEvaluateSingleExpressionTemplate ¶ added in v0.3.0
func TryEvaluateSingleExpressionTemplate(template string, context map[string]interface{}) (interface{}, bool, bool, error)
TryEvaluateSingleExpressionTemplate determines if the provided template consists of exactly one expression node (ignoring surrounding whitespace and comments), and if so, evaluates it and returns the typed value. It returns: - value: the evaluated value of the single expression - isSingle: whether the template is a single expression template - wasUndefined: whether the expression evaluated to a strictly undefined value - err: any parsing or evaluation error encountered
Types ¶
type ControlTagInfo ¶
type ControlTagInfo struct { Type ControlTagType Expression string // For 'if', 'elif', 'for': the condition or loop expression. }
ControlTagInfo holds detailed information about a parsed control tag.
type ControlTagType ¶
type ControlTagType string
ControlTagType defines the specific type of a control tag.
const ( ControlIf ControlTagType = "if" ControlEndIf ControlTagType = "endif" ControlFor ControlTagType = "for" // Placeholder for future 'for' loop implementation ControlEndFor ControlTagType = "endfor" // Placeholder for future 'endfor' implementation ControlElse ControlTagType = "else" // Placeholder for future 'else' implementation ControlElseIf ControlTagType = "elif" // Placeholder for future 'elif' (else if) implementation ControlInclude ControlTagType = "include" ControlUnknown ControlTagType = "unknown" )
Enumerates the different types of control tags.
type EvaluateExpressionFunc ¶
type EvaluateExpressionFunc func(expression string, context map[string]interface{}) (interface{}, error)
EvaluateExpressionFunc defines the signature for an expression evaluation function. This is used to pass EvaluateExpression logic to statement handlers.
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator evaluates an AST for a Jinja expression using a provided context.
func NewEvaluator ¶
NewEvaluator creates a new evaluator with a merged context (user context + global functions).
type ExprNode ¶
type ExprNode struct { Type ExprNodeType Value interface{} Children []*ExprNode Operator string Identifier string FilterName string FilterArgs []*ExprNode }
ExprNode represents a node in the expression AST
type ExprNodeType ¶
type ExprNodeType int
ExprNodeType represents the type of AST node
const ( NodeLiteral ExprNodeType = iota NodeIdentifier NodeUnaryOp NodeBinaryOp NodeAttribute NodeSubscript NodeFunctionCall NodeList NodeDict NodeTuple NodeFilterChain )
type ExprParser ¶
type ExprParser struct {
// contains filtered or unexported fields
}
ExprParser parses a sequence of tokens into an abstract syntax tree (AST) for Jinja expressions.
func NewExprParser ¶
func NewExprParser(tokens []Token) *ExprParser
NewExprParser creates a new parser for the given tokens.
func (*ExprParser) Parse ¶
func (p *ExprParser) Parse() (*ExprNode, error)
Parse parses the tokens into an AST root node.
type FileResolver ¶ added in v0.3.0
FileResolver resolves file contents for include/lookup with custom search rules
type FilterFunc ¶
type FilterFunc func(input interface{}, args ...interface{}) (interface{}, error)
FilterFunc defines the signature for a filter function. input is the value to be filtered. args are the arguments passed to the filter.
type FunctionFunc ¶
FunctionFunc defines the signature for a function callable from templates
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer is responsible for breaking an input string into tokens for parsing Jinja expressions.
type Node ¶
type Node struct { Type NodeType // The kind of node (e.g., text, expression). Content string // The raw content of the node. Control *ControlTagInfo // Populated if Type is NodeControlTag, provides details about the control tag. }
Node represents a parsed element in the template, such as literal text or an expression.
type NodeType ¶
type NodeType int
NodeType defines the category of a parsed Node.
const ( NodeText NodeType = iota // Represents a segment of literal text. NodeExpression // Represents a Jinja expression, e.g., {{ variable }}. NodeComment // Represents a comment, e.g., {# comment #}. NodeControlTag // Represents a control structure tag, e.g., {% if ... %}. )
Enumerates the different types of nodes that can be encountered during parsing.
type OmitType ¶ added in v0.3.0
type OmitType struct{}
OmitType is a sentinel type for the 'omit' keyword in Jinja expressions It is used in filters like default(omit) to indicate that a value should be omitted
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser holds the state of the parsing process. It is used to iteratively parse a template string into a sequence of nodes.
type ProcessNodesFunc ¶
ProcessNodesFunc defines the signature for the node processing function. This is used to allow statement handlers to recursively process blocks of nodes.
type TemplateCache ¶
type TemplateCache struct {
// contains filtered or unexported fields
}
TemplateCache is a thread-safe cache for parsed templates
func NewTemplateCache ¶
func NewTemplateCache() *TemplateCache
NewTemplateCache creates a new template cache
func (*TemplateCache) Get ¶
func (tc *TemplateCache) Get(template string) ([]*Node, bool)
Get retrieves parsed nodes for a template from the cache
func (*TemplateCache) Set ¶
func (tc *TemplateCache) Set(template string, nodes []*Node)
Set stores parsed nodes for a template in the cache
type TokenType ¶
type TokenType int
TokenType represents different types of tokens in a Jinja expression
type UndefinedType ¶ added in v0.1.8
type UndefinedType struct{}
UndefinedType is a sentinel type for undefined variables in Jinja expressions It is used to distinguish between nil (which is a valid value) and undefined (which is not set in context)