Skip to content

✨ Add block assignment functionality in jinja package. #1341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/jinja/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class SetStatement extends Statement {
override type = "Set";
constructor(
public assignee: Expression,
public value: Expression
public value: Expression | null,
public body: Statement[]
) {
super();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/jinja/src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const TOKEN_TYPES = Object.freeze({
Is: "Is",
NotIn: "NotIn",
Else: "Else",
EndSet: "EndSet",
EndIf: "EndIf",
ElseIf: "ElseIf",
EndFor: "EndFor",
Expand All @@ -61,6 +62,7 @@ const KEYWORDS = Object.freeze({
is: TOKEN_TYPES.Is,
if: TOKEN_TYPES.If,
else: TOKEN_TYPES.Else,
endset: TOKEN_TYPES.EndSet,
endif: TOKEN_TYPES.EndIf,
elif: TOKEN_TYPES.ElseIf,
endfor: TOKEN_TYPES.EndFor,
Expand Down
19 changes: 16 additions & 3 deletions packages/jinja/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,24 @@ export function parse(tokens: Token[]): Program {

if (is(TOKEN_TYPES.Equals)) {
++current;
const value = parseSetStatement();
const value = parseExpression();

return new SetStatement(left, value, []);
} else {
// parsing multiline set here
const body: Statement[] = [];
expect(TOKEN_TYPES.CloseStatement, "Expected %} token");
while (
!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndSet)
) {
const another = parseAny();
body.push(another);
}
expect(TOKEN_TYPES.OpenStatement, "Expected {% token");
expect(TOKEN_TYPES.EndSet, "Expected endset token");

return new SetStatement(left, value);
return new SetStatement(left, null, body);
}
return left;
}

function parseIfStatement(): If {
Expand Down
4 changes: 3 additions & 1 deletion packages/jinja/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ export class Interpreter {
);
case "join":
return new StringValue(operand.value.map((x) => x.value).join(""));
case "string":
return new StringValue(toJSON(operand));
default:
throw new Error(`Unknown ArrayValue filter: ${filter.value}`);
}
Expand Down Expand Up @@ -916,7 +918,7 @@ export class Interpreter {
}

private evaluateSet(node: SetStatement, environment: Environment): NullValue {
const rhs = this.evaluate(node.value, environment);
const rhs = node.value ? this.evaluate(node.value, environment) : this.evaluateBlock(node.body, environment);
if (node.assignee.type === "Identifier") {
const variableName = (node.assignee as Identifier).value;
environment.setVariable(variableName, rhs);
Expand Down
10 changes: 10 additions & 0 deletions packages/jinja/test/e2e.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions packages/jinja/test/templates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const TEST_STRINGS = {
// Set variables
VARIABLES: `{% set x = 'Hello' %}{% set y = 'World' %}{{ x + ' ' + y }}`,
VARIABLES_2: `{% set x = 'Hello'.split('el')[-1] %}{{ x }}`,
VARIABLES_BLOCK: `{% set x %}Hello!\nMultiline/block set!\n{% endset %}{{ x }}`,

// Numbers
NUMBERS: `|{{ 5 }}|{{ -5 }}|{{ add(3, -1) }}|{{ (3 - 1) + (a - 5) - (a + 5)}}|`,
Expand Down Expand Up @@ -90,6 +91,7 @@ const TEST_STRINGS = {
FILTER_OPERATOR_10: `|{{ " 1 \n 2 \n 3 \n\n " | indent }}|{{ " 1 \n 2 \n 3 \n\n " | indent(2) }}|{{ " 1 \n 2 \n 3 \n\n " | indent(first=True) }}|{{ " 1 \n 2 \n 3 \n\n " | indent(blank=True) }}|{{ " 1 \n 2 \n 3 \n\n " | indent(4, first=True) }}|`,
FILTER_OPERATOR_11: `{{ items | rejectattr('key') | length }}`,
FILTER_OPERATOR_12: `{{ messages | rejectattr('role', 'equalto', 'system') | length }}`,
FILTER_OPERATOR_13: `{{ tools | string }}`,

// Logical operators between non-Booleans
BOOLEAN_NUMERICAL: `|{{ 1 and 2 }}|{{ 1 and 0 }}|{{ 0 and 1 }}|{{ 0 and 0 }}|{{ 1 or 2 }}|{{ 1 or 0 }}|{{ 0 or 1 }}|{{ 0 or 0 }}|{{ not 1 }}|{{ not 0 }}|`,
Expand Down Expand Up @@ -705,6 +707,19 @@ const TEST_PARSED = {
{ value: "x", type: "Identifier" },
{ value: "}}", type: "CloseExpression" },
],
VARIABLES_BLOCK: [
{ value: "{%", type: "OpenStatement" },
{ value: "set", type: "Set" },
{ value: "x", type: "Identifier" },
{ value: "%}", type: "CloseStatement" },
{ value: `Hello!\nMultiline/block set!\n`, type: "Text" },
{ value: "{%", type: "OpenStatement" },
{ value: "endset", type: "EndSet" },
{ value: "%}", type: "CloseStatement" },
{ value: "{{", type: "OpenExpression" },
{ value: "x", type: "Identifier" },
{ value: "}}", type: "CloseExpression" },
],

// Numbers
NUMBERS: [
Expand Down Expand Up @@ -1682,6 +1697,13 @@ const TEST_PARSED = {
{ value: "length", type: "Identifier" },
{ value: "}}", type: "CloseExpression" },
],
FILTER_OPERATOR_13: [
{ value: "{{", type: "OpenExpression" },
{ value: "tools", type: "Identifier" },
{ value: "|", type: "Pipe" },
{ value: "string", type: "Identifier" },
{ value: "}}", type: "CloseExpression" },
],

// Logical operators between non-Booleans
BOOLEAN_NUMERICAL: [
Expand Down Expand Up @@ -3038,6 +3060,7 @@ const TEST_CONTEXT = {
// Set variables
VARIABLES: {},
VARIABLES_2: {},
VARIABLES_BLOCK: {},

// Numbers
NUMBERS: {
Expand Down Expand Up @@ -3173,6 +3196,9 @@ const TEST_CONTEXT = {
FILTER_OPERATOR_12: {
messages: [{ role: "system" }, { role: "user" }, { role: "assistant" }],
},
FILTER_OPERATOR_13: {
tools: [{ name: "some_tool", arguments: { some_name: "string" } }],
},

// Logical operators between non-Booleans
BOOLEAN_NUMERICAL: {},
Expand Down Expand Up @@ -3300,6 +3326,7 @@ const EXPECTED_OUTPUTS = {
// Set variables
VARIABLES: "Hello World",
VARIABLES_2: "lo",
VARIABLES_BLOCK: "Hello!\nMultiline/block set!\n",

// Numbers
NUMBERS: "|5|-5|2|-8|",
Expand Down Expand Up @@ -3353,6 +3380,7 @@ const EXPECTED_OUTPUTS = {
FILTER_OPERATOR_10: `| 1 \n 2 \n 3 \n\n | 1 \n 2 \n 3 \n\n | 1 \n 2 \n 3 \n\n | 1 \n 2 \n 3 \n \n | 1 \n 2 \n 3 \n\n |`,
FILTER_OPERATOR_11: `3`,
FILTER_OPERATOR_12: `2`,
FILTER_OPERATOR_13: `[{"name": "some_tool", "arguments": {"some_name": "string"}}]`,

// Logical operators between non-Booleans
BOOLEAN_NUMERICAL: `|2|0|0|0|1|1|1|0|false|true|`,
Expand Down