Skip to content

Commit

Permalink
Fixed issue with for loop only rendering once if multiple instances w…
Browse files Browse the repository at this point in the history
…ere in same template
  • Loading branch information
mbosecke committed Mar 2, 2015
1 parent 40cb4f6 commit 93d0ddc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v1.4.3
- Bug fix for issue regarding multiple for loops only rendering first one

## v1.4.2
- Performance improvements

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ First, add the following dependency to your pom.xml:
<dependency>
<groupId>com.mitchellbosecke</groupId>
<artifactId>pebble</artifactId>
<version> 1.4.2 </version>
<version> 1.4.3 </version>
</dependency>
```

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/mitchellbosecke/pebble/node/ForNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public void render(PebbleTemplateImpl self, Writer writer, EvaluationContext con
iterable = toIterable(iterableEvaluation);

Iterator<?> iterator = iterable.iterator();

boolean newScope = false;

if (iterator.hasNext()) {

Expand All @@ -72,6 +74,7 @@ public void render(PebbleTemplateImpl self, Writer writer, EvaluationContext con
*/
if (context.currentScopeContainsVariable("loop") || context.currentScopeContainsVariable(variableName)) {
context.pushScope();
newScope = true;
}

int length = getIteratorSize(iterableEvaluation);
Expand All @@ -94,12 +97,15 @@ public void render(PebbleTemplateImpl self, Writer writer, EvaluationContext con
body.render(self, writer, context);

}

if(newScope){
context.popScope();
}

} else if (elseBody != null) {
elseBody.render(self, writer, context);
}

context.popScope();
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/mitchellbosecke/pebble/CoreTagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public void multipleForLoops() throws PebbleException, IOException {
Loader loader = new StringLoader();
PebbleEngine pebble = new PebbleEngine(loader);

String source = "" + "{% for user in users %}{% endfor %}" + "{% for user in users %}{% endfor %}";
String source = "" + "{% for user in users %}{{ user.username }}{% endfor %}" + "{% for user in users %}{{ user.username }}{% endfor %}";
PebbleTemplate template = pebble.getTemplate(source);
Map<String, Object> context = new HashMap<>();
List<User> users = new ArrayList<>();
Expand All @@ -328,6 +328,8 @@ public void multipleForLoops() throws PebbleException, IOException {

Writer writer = new StringWriter();
template.evaluate(writer, context);

assertEquals("AlexBobAlexBob", writer.toString());
}

@Test
Expand Down

0 comments on commit 93d0ddc

Please sign in to comment.