diff --git a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java index 0c87441a1..2c41d57e5 100644 --- a/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java +++ b/src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java @@ -40,6 +40,7 @@ import com.hubspot.jinjava.objects.serialization.PyishSerializable; import com.hubspot.jinjava.random.ConstantZeroRandomNumberGenerator; import com.hubspot.jinjava.random.DeferredRandomNumberGenerator; +import com.hubspot.jinjava.tree.ExpressionNode; import com.hubspot.jinjava.tree.Node; import com.hubspot.jinjava.tree.TagNode; import com.hubspot.jinjava.tree.TreeParser; @@ -259,7 +260,7 @@ public String render(Node root, boolean processExtendRoots) { position = node.getStartPosition(); String renderStr = node.getMaster().getImage(); try { - if (context.doesRenderStackContain(renderStr)) { + if (node instanceof ExpressionNode && context.doesRenderStackContain(renderStr)) { // This is a circular rendering. Stop rendering it here. addError( new TemplateError( diff --git a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java index a955ffb60..ca613ef99 100644 --- a/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java +++ b/src/test/java/com/hubspot/jinjava/tree/ExpressionNodeTest.java @@ -72,6 +72,25 @@ public void itAvoidsInfiniteRecursionWhenVarsContainBraceBlocks() throws Excepti assertThat(node.render(interpreter).toString()).isEqualTo("hello {{ place }}"); } + @Test + public void itAllowsNestedTagExpressions() throws Exception { + context.put("myvar", "{% if true %}{{ place }}{% endif %}"); + context.put("place", "{% if true %}Hello{% endif %}"); + + ExpressionNode node = fixture("simplevar"); + assertThat(node.render(interpreter).toString()).isEqualTo("Hello"); + } + + @Test + public void itAvoidsInfiniteRecursionWhenVarsAreInIfBlocks() throws Exception { + context.put("myvar", "{% if true %}{{ place }}{% endif %}"); + context.put("place", "{% if true %}{{ myvar }}{% endif %}"); + + ExpressionNode node = fixture("simplevar"); + assertThat(node.render(interpreter).toString()) + .isEqualTo("{% if true %}{{ myvar }}{% endif %}"); + } + @Test public void itDoesNotRescursivelyEvaluateExpressionsOfSelf() throws Exception { context.put("myvar", "hello {{myvar}}");