Skip to content

Commit

Permalink
Last minute change of t:template to t:object, makes more sense.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Mar 10, 2024
1 parent 6f86df0 commit ce3a746
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Feature complete. Just some test coverage to complete and addition of Javadoc.
* No dependencies, JPMS compliant, Graal Native Image friendly
* Fast. See Design Choices.
* Simple Java. Public API consists of just 2 main classes, `TemplateModel` and `TemplateProcessor`.
* Simple Content. Just `<t:if>` (and `<t:else>`), `<t:include>`, `<t:template>` and `<t:list>`. Bash like variable such as `${myVar}`.
* Simple Content. Just `<t:if>` (and `<t:else>`), `<t:include>`, `<t:object>` and `<t:list>`. Bash like variable such as `${myVar}`.
* Internationalisation features.

## Design Choices
Expand Down Expand Up @@ -90,11 +90,11 @@ public class Example1 {
<ul>
</t:if>
<t:template me>
<t:object me>
<p>Name: ${name}</p>
<p>Age: ${age}</p>
<p>Location: ${location}</p>
</t:template>
</t:object>
</body>
</html>
""").
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/sshtools/tinytemplate/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ else if (ch == 't') {
case T_TAG_NAME:
if (ch == '>') {
var directive = buf.toString().substring(1).trim();
if(directive.startsWith("t:if ") || directive.startsWith("t:list ")) {
if(directive.startsWith("t:if ") || directive.startsWith("t:list ") || directive.startsWith("t:object ")) {
block.nestDepth++;
}
if(process) {
Expand Down Expand Up @@ -999,14 +999,14 @@ else if(dir.equals("t:else")) {
block.state = State.START;
return true;
}
else if(dir.equals("t:template")) {
else if(dir.equals("t:object")) {
var templateSupplier = block.model.templates.get(var);
if (templateSupplier == null) {
logger.ifPresent(l -> l.warning("Missing template {0} in message template", var));
return false;
}
else {
var templBlock = new Block(block.model, block.expander, block.reader, "template", true);
var templBlock = new Block(block.model, block.expander, block.reader, "object", true);
templBlock.nestDepth = 1;
templBlock.capture = true;
read(templBlock);
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/sshtools/tinytemplate/TemplatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,66 @@ public void testTemplateNestedIf3() {

}

@Test
public void testTemplateObjectMissing() {
Assertions.assertEquals("""
<html>
<body>
</body>
</html>
""",
createParser().process(TemplateModel.ofContent("""
<html>
<body>
<t:object aTemplate>
<p>Should not be visible</p>
<p>Name: ${name}</p>
</t:object>
</body>
</html>
""").
variable("name", "Joe B")));

}

@Test
public void testTemplateObject() {
Assertions.assertEquals("""
<html>
<body>
<p>Some text</p>
<p>Name: Joe B</p>
<p>Age: 27</p>
<p>Location: London</p>
<p>Some other text</p>
</body>
</html>
""",
createParser().process(TemplateModel.ofContent("""
<html>
<body>
<p>Some text</p>
<t:object aPerson>
<p>Name: ${name}</p>
<p>Age: ${age}</p>
<p>Location: ${location}</p>
</t:object>
<p>Some other text</p>
</body>
</html>
""").
template("aPerson", (c) ->
TemplateModel.ofContent(c).
variable("name", "Joe B").
variable("age", "27").
variable("location", "London")
)));

}

private TemplateProcessor createParser() {
return new TemplateProcessor.Builder().build();
}
Expand Down

0 comments on commit ce3a746

Please sign in to comment.