Skip to content

Commit

Permalink
Merge pull request #23233 from mkouba/int-iterable
Browse files Browse the repository at this point in the history
Qute type-safe templates - int (primitive) is also iterable
  • Loading branch information
gastaldi authored Jan 27, 2022
2 parents 35a7cda + b5dd29e commit 51d105c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/qute-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ A section helper that defines the logic of a section can "execute" any of the bl
[[loop_section]]
==== Loop Section

The loop section makes it possible to iterate over an instance of `Iterable`, `Iterator`, array, `Map` 's entry set, `Stream` and an `Integer`.
The loop section makes it possible to iterate over an instance of `Iterable`, `Iterator`, array, `Map` (element is a `Map.Entry`), `Stream`, `Integer` and `int` (primitive value).
It has two flavors.
The first one is using the `each` name and `it` is an implicit alias for the iteration element.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,9 @@ private static Expression findExpression(String helperHint, String hintPrefix, T

static void processLoopElementHint(Match match, IndexView index, Expression expression,
BuildProducer<IncorrectExpressionBuildItem> incorrectExpressions) {
if (match.isEmpty() || match.type().name().equals(DotNames.INTEGER)) {
if (match.isEmpty()
|| match.type().name().equals(DotNames.INTEGER)
|| match.type().equals(PrimitiveType.INT)) {
return;
}
Type matchType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,35 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.test.QuarkusUnitTest;

public class LoopTest {

static StringAsset template = new StringAsset("{#for i in total}{i}:{/for}");

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addAsResource(new StringAsset("{#for i in total}{i}:{/for}"), "templates/loop1.html"));
.withApplicationRoot(root -> root
.addAsResource(template, "templates/loop1.html")
.addAsResource(template, "templates/LoopTest/loopInt.html"));

@CheckedTemplate
static class Templates {

static native TemplateInstance loopInt(int total);

}

@Inject
Template loop1;

@Test
public void testIntegerIsIterable() {
assertEquals("1:2:3:", loop1.data("total", 3).render());
assertEquals("1:2:3:", Templates.loopInt(3).render());
}

}

0 comments on commit 51d105c

Please sign in to comment.