TemplateContext class abstract interface

Represents the runtime context for a template, providing the mechanisms to resolve variables and evaluate expressions during template rendering.

A TemplateContext abstracts how template variables and expressions are resolved, enabling dynamic rendering of templates with placeholders, conditionals, loops, and filters.

Responsibilities

  • Provide a TemplateVariableResolver to map template variable names to their runtime string representations.
  • Provide a TemplateExpressionEvaluator to evaluate expressions, supporting logical operations, comparisons, and conditional flow.
  • Serve as the bridge between raw template content and the actual data used to populate it.

Design Notes

  • Implementations may wrap attribute maps, computed values, or external sources for dynamic resolution.
  • Should ensure consistency between variable resolution and attributes.
  • Must handle invalid variable names or expressions gracefully, either by returning default values or throwing descriptive exceptions.

Example Implementation

class DefaultTemplateContext implements TemplateContext {
  final Map<String, Object?> _attributes;
  final TemplateVariableResolver _resolver;
  final TemplateExpressionEvaluator _evaluator;

  DefaultTemplateContext(this._attributes)
      : _resolver = MapVariableResolver(_attributes),
        _evaluator = SimpleExpressionEvaluator(_attributes);

  @override
  TemplateVariableResolver getVariableResolver() => _resolver;

  @override
  TemplateExpressionEvaluator getExpressionEvaluator() => _evaluator;
}

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

getExpressionEvaluator() → TemplateExpressionEvaluator
Returns a TemplateExpressionEvaluator capable of evaluating expressions within the template context.
getVariableResolver() → TemplateVariableResolver
Returns a TemplateVariableResolver capable of resolving template variable names into their string representations.
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