Evaluates expressions in the context of a template, returning a boolean result that indicates whether the expression is truthy.
An TemplateExpressionEvaluator is used within template engines to support conditional rendering, loops, or dynamic content based on the values in a TemplateContext.
Responsibilities
- Parse and evaluate expressions provided as strings.
- Resolve variables and values using a TemplateContext.
- Return a boolean indicating whether the expression is considered "truthy".
- Support template logic such as
ifstatements,forloops, or custom conditional directives.
Design Notes
- Implementations may use expression parsing libraries, regex evaluation, or custom interpreters.
- Should handle unknown variables gracefully, e.g., treat missing variables
as
nullor false, depending on the template logic. - Can be extended to support operators, comparisons, and functions within template expressions.
Usage Example
final evaluator = MyExpressionEvaluator();
final context = MyTemplateContext({'isLoggedIn': true});
final result = evaluator.evaluate('isLoggedIn', context);
print(result); // true
final result2 = evaluator.evaluate('user.age > 18', context);
print(result2); // true or false depending on context
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
evaluate(
String expression, TemplateContext context) → bool -
Evaluates the given
expressionstring using the providedcontext. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited