Skip to content

Commit

Permalink
Regression for t:object.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Mar 15, 2024
1 parent b0a7a83 commit 43102bd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/sshtools/tinytemplate/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ else if (ch == 't') {
if(logger.isPresent())
logger.get().debug(formatDebug(block, "Leaf end {0}. Process {1}, Match {2}. Nest Depth {3}"), directive, process, block.match, block.nestDepth);

if(directive.equals("t:else")) {
if(!block.capture && directive.equals("t:else")) {
var parentMatch = block.nestDepth > 1 ? block.match : block.parent == null || block.parent.match;
var newMatch = !block.match && parentMatch;
if(logger.isPresent())
Expand Down Expand Up @@ -1143,7 +1143,7 @@ else if(dir.equals("t:object")) {
var was = templ.parent;
try {
templ.parent = Optional.of(block.model);
var listBlock = new Block(block, templ,getExpanderForModel(templ), templ.text(true));
var listBlock = new Block(block, templ,getExpanderForModel(templ), templ.text(true), "object", true);
read(listBlock);
block.out.append(listBlock.out.toString());
}
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/sshtools/tinytemplate/TemplatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,69 @@ public void testTemplateIncludeMorThanOnce() {

}

@Test
public void testTemplateObjectInCondition() {
Assertions.assertEquals("""
<p>Some text</p>
<p>Name: Joe B</p>
<p>Some other text</p>
""",
createParser().process(TemplateModel.ofContent("""
<p>Some text</p>
<t:if aPerson>
<t:object aPerson>
<p>Name: ${name}</p>
</t:object>
</t:if>
<p>Some other text</p>
""").
object("aPerson", (c) ->
TemplateModel.ofContent(c).
variable("name", "Joe B")
)));

}

@Test
public void testTemplateFlaseConditionInObjectInTrueCondition() {
Assertions.assertEquals("""
<p>Some text</p>
<p>Name: Joe B</p>
<p>No Age</p>
<p>Some other text</p>
""",
createParser().process(TemplateModel.ofContent("""
<p>Some text</p>
<t:if aPerson>
<t:object aPerson>
<p>Name: ${name}</p>
<t:if aAge>
<p>Age: ${aAge}</p>
<t:else/>
<p>No Age</p>
</t:if>
</t:object>
</t:if>
<p>Some other text</p>
""").
object("aPerson", (c) ->
TemplateModel.ofContent(c).
variable("name", "Joe B").
variable("aAge", "")
)));

}

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

0 comments on commit 43102bd

Please sign in to comment.