ast library
Parses Jinja templates into an AST for inspection without rendering them.
parseTemplate returns the root Program; walk it by pattern-matching on
the node types this library exports. package:dinja/dinja.dart remains the
entry point for rendering.
import 'package:dinja/ast.dart';
final program = parseTemplate('{{ name }}');
for (final statement in program.body) {
print(statement.type);
}
BinaryExpression.op and UnaryExpression.op are lexer tokens; read the
operator text, such as == or not, from their value. The Token and
TokenType types themselves are not exported.
This library reads a parsed template; it does not evaluate one.
Statement.execute cannot be called through it, because the type of its
ctx parameter is not exported by any dinja library. Render with
Template from package:dinja/dinja.dart instead.
Classes
- ArrayLiteral
- Represents an array (list) literal.
- BinaryExpression
-
Represents a binary operation (e.g.,
+,-,*,/,==,!=). - BlankExpression
-
Represents an empty subscript, as in
a[], which evaluates to undefined. - BreakStatement
- Represents a 'break' statement within a loop.
- CallExpression
- Represents a function or macro call expression.
- CallStatement
- Represents a 'call' block, used to invoke a macro with block content.
- CommentStatement
- Represents a comment in the Jinja source.
- ContinueStatement
- Represents a 'continue' statement within a loop.
- DoStatement
- Represents a 'do' statement, which evaluates an expression and ignores its result.
- Expression
- Base class for AST nodes that evaluate to a value.
- FilterExpression
-
Represents a filter application (e.g.,
val | filter). - FilterStatement
- Represents a 'filter' block, applying a filter to its content.
- FloatLiteral
- Represents a floating-point literal.
- ForStatement
- Represents a 'for' loop over an iterable.
- Identifier
- Represents an identifier (variable name).
- IfStatement
- Represents a conditional 'if' block.
- IntegerLiteral
- Represents an integer literal.
- KeywordArgumentExpression
- Represents a keyword argument in a function or macro call.
- MacroStatement
- Represents a 'macro' definition.
- MemberExpression
-
Represents a member access expression (e.g.,
obj.proporobj[expr]). - NoopStatement
- A statement that does nothing.
- ObjectLiteral
- Represents an object (map) literal.
- Program
- The root node of a Jinja template AST, containing a body of statements.
- SelectExpression
- Represents a selection expression used in filtering iterations.
- SetStatement
- Represents a 'set' statement for variable assignment.
- SliceExpression
-
Represents a slice expression (e.g.,
start:stop:step). - SpreadExpression
- Represents a spread operation.
- Statement
- Base class for all nodes in the Jinja AST that represent a command or structure.
- StringLiteral
- Represents a string literal.
- TernaryExpression
-
Represents a ternary conditional expression (e.g.,
a if cond else b). - TestExpression
-
Represents a test application (e.g.,
val is defined). - TupleLiteral
- Represents a tuple literal.
- UnaryExpression
-
Represents a unary operation (e.g.,
not,-,+).
Functions
-
parseTemplate(
String source) → Program -
Parses the Jinja template
sourceinto a Program without rendering it.
Exceptions / Errors
- LexerException
- Exception thrown when a lexical error is encountered.
- ParserException
- Exception thrown when a parsing error is encountered.