DefaultTemplateRenderer class final

A template renderer that converts Template instances into fully-rendered source code strings (SourceCode) using context-aware processing, variable substitution, conditionals, loops, and filters.

This renderer is part of a template processing framework and works with a TemplateFilterRegistry to apply custom transformations to variables during rendering.

Features

  • Parses template structure to identify includes, conditionals, loops, and variables.
  • Renders templates with context-aware variable resolution.
  • Supports nested templates, conditional blocks, loops, and custom filters.
  • Escapes HTML by default to prevent XSS vulnerabilities.
  • Returns a SourceCode object containing raw content, rendered content, and code structure.

Template Syntax

  • Includes: {{> templateName}}
  • Conditionals: {{#if condition}}content{{/if}}
  • Loops: {{#each items}}content{{/each}}
  • Variables: {{variableName}}
  • Variables with filters: {{variableName | filter1 | filter2}}

Example Usage

final renderer = DefaultTemplateRenderer(filterRegistry);
final template = Template(
  getLocation: () => '<h1>{{title}}</h1>',
  getContext: () => DefaultTemplateContext({'title': 'Hello World'}),
);

final result = renderer.render(template);
print(result.renderedContent); // <h1>Hello World</h1>

Processing Order

  1. Includes ({{> ...}})
  2. Conditional blocks ({{#if ...}} ... {{/if}})
  3. Loops ({{#each ...}} ... {{/each}})
  4. Filters ({{variable | filter1 | filter2}})
  5. Variables ({{variable}})

Design Notes

  • Simple regex-based parsing for structural elements (includes, conditionals, loops).
  • Context cloning ensures each loop iteration has isolated variables like this, @index, @first, @last.
  • Filter processing uses the provided TemplateFilterRegistry to transform variable output.
  • HTML escaping applied to variable values by default.

See Also

Implemented types

Constructors

DefaultTemplateRenderer(TemplateFilterRegistry _filterRegistry, AssetBuilder _assetBuilder)
A template renderer that converts Template instances into fully-rendered source code strings (SourceCode) using context-aware processing, variable substitution, conditionals, loops, and filters.

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

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
render(Template template, TemplateContext context, [Asset? built]) SourceCode
Renders the given template using its associated TemplateContext.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited