Skip to content

Commit 62fa77a

Browse files
committed
Preserve the setting of a variable when calling eager macro function.
1 parent 1133893 commit 62fa77a

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/main/java/com/hubspot/jinjava/lib/tag/ForTag.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
**********************************************************************/
1616
package com.hubspot.jinjava.lib.tag;
1717

18+
import static com.hubspot.jinjava.util.Logging.ENGINE_LOG;
19+
1820
import com.google.common.collect.Lists;
1921
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
2022
import com.hubspot.jinjava.doc.annotations.JinjavaHasCodeBody;
@@ -165,6 +167,7 @@ public String renderForCollection(
165167
Object collection
166168
) {
167169
ForLoop loop = ObjectIterator.getLoop(collection);
170+
int loopCount = 0;
168171

169172
try (InterpreterScopeClosable c = interpreter.enterScope()) {
170173
if (interpreter.isValidationMode() && !loop.hasNext()) {
@@ -178,6 +181,7 @@ public String renderForCollection(
178181
interpreter.getConfig().getMaxOutputSize()
179182
);
180183
while (loop.hasNext()) {
184+
++loopCount;
181185
Object val;
182186
try {
183187
val = interpreter.wrap(loop.next());
@@ -274,6 +278,8 @@ public String renderForCollection(
274278
}
275279
}
276280
return checkLoopVariable(interpreter, buff);
281+
} finally {
282+
ENGINE_LOG.error("Loop count {}", loopCount);
277283
}
278284
}
279285

src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerCallTag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String eagerInterpret(
4141
InterpretException e
4242
) {
4343
interpreter.getContext().checkNumberOfDeferredTokens();
44-
try (InterpreterScopeClosable c = interpreter.enterScope()) {
44+
try (InterpreterScopeClosable c = interpreter.enterNonStackingScope()) {
4545
MacroFunction caller = new MacroFunction(
4646
tagNode.getChildren(),
4747
"caller",

src/test/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunctionTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ public void itReconstructsForAliasedName() {
7777
.isEqualTo(String.format(codeFormat, fullName));
7878
}
7979

80+
@Test
81+
public void itResolvesFromSet() {
82+
String template =
83+
"{% macro foo(foobar, other) %}" +
84+
" {% do foobar.update({'a': 'b'} ) %} " +
85+
" {{ foobar }} and {{ other }}" +
86+
"{% endmacro %}" +
87+
"{% set bar = {} %}" +
88+
"{% call foo(bar, deferred) %} {% endcall %}" +
89+
"{{ bar }}";
90+
String firstPass = interpreter.render(template);
91+
assertThat(firstPass).isEqualTo(template);
92+
}
93+
8094
@Test
8195
public void itReconstructsImageWithNamedParams() {
8296
String name = "foo";

0 commit comments

Comments
 (0)