execute method
Executes the statement within the given ctx and returns the result as a JinjaValue.
Implementation
@override
JinjaValue execute(Context ctx) {
final val = operand.execute(ctx);
// Resolve filter function
String filterName;
List<JinjaValue> args = [val]; // Filter receives value as first arg
Map<String, JinjaValue> kwargs = {};
if (filter is Identifier) {
filterName = (filter as Identifier).name;
} else if (filter is CallExpression) {
final call = filter as CallExpression;
if (call.callee is! Identifier) {
throw Exception('Filter must be identifier');
}
filterName = (call.callee as Identifier).name;
// Evaluate args
for (final arg in call.args) {
if (arg is KeywordArgumentExpression) {
kwargs[(arg.key as Identifier).name] = arg.val.execute(ctx);
} else if (arg is Expression) {
args.add(arg.execute(ctx));
}
}
} else {
throw Exception('Invalid filter expression');
}
if (globalFilters.containsKey(filterName)) {
final res = globalFilters[filterName]!(args, kwargs);
return deriveRendered(res, [...args, ...kwargs.values]);
}
throw Exception('Unknown filter: $filterName');
}