Skip to content

Commit

Permalink
Fixes messed up condition block handling, release 0.9.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Oct 7, 2023
1 parent d5090cd commit 5455966
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sshtools</groupId>
<artifactId>tinytemplate</artifactId>
<version>0.9.1</version>
<version>0.9.2</version>
<name>TinyTemplate</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/sshtools/tinytemplate/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ else if (ch == 't') {
logger.ifPresent(lg -> lg.debug("Leaving scope {0}", block.scope));
return;
} else {
if(isIf) {
if(directive.equals(block.scope) && isIf) {
buf.setLength(0);
}
else {
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/com/sshtools/tinytemplate/TemplatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,98 @@ public void testTemplateIfWithVariables() {
variable("var1", "Some Name")));
}


@Test
public void testTemplateIncludeWithList() {
Assertions.assertEquals("""
<html>
<body>
<div class="alert alert-style-0" role="alert">
Title 0
</div>
<div class="alert alert-style-1" role="alert">
Title 1
</div>
<div class="alert alert-style-2" role="alert">
Title 2
</div>
<div class="alert alert-style-3" role="alert">
<i class="bi bi-icon-3"></i>
Title 3
</div>
<div class="alert alert-style-4" role="alert">
<i class="bi bi-icon-4"></i>
Title 4
<br/>
<small>Description4</small>
</div>
</body>
</html>
""",
createParser().process(TemplateModel.ofContent(
"""
<html>
<body>
<t:if alerts>
<t:include alerts />
</t:if>
</body>
</html>
""").
include("alerts",
TemplateModel.ofContent("""
<t:list items>
<div class="alert alert-${style}" role="alert">
<t:if icon>
<i class="bi bi-${icon}"></i>
</t:if>
${title}
<t:if description>
<br/>
<small>${description}</small>
</t:if>
</div>
</t:list>
""").list("items", (content) -> {
var l = new ArrayList<TemplateModel>();
for(int i = 0 ; i < 5 ; i++) {
var itemMdl = TemplateModel.ofContent(content).
variable("style", "style-" + i).
variable("title", "Title " + i);
if(i > 2)
itemMdl.variable("icon", "icon-" + i);
if(i > 3)
itemMdl.variable("description", "Description" + i);
l.add(itemMdl);
}
return l;
})
)
).stripIndent());
}

@Test
public void testTemplateIfWithInclude() {
Assertions.assertEquals("""
Expand Down

0 comments on commit 5455966

Please sign in to comment.